I'm trying to separate some strings by different criteria but I can't get the desired results.
Here are 3 examples:
$ppl[0] = "Balko, Vlado \"Panelбk\" (2008) {Byt na tretom (#1.55)}";
$ppl[1] = "'Abd Al-Hamid, Ja'far A Two Hour Delay (2001)";
$ppl[2] = "'t Hoen, Frans De reьnie (1963) (TV)";
I'm currently using this for the last 2:
$pattern = '#,|\t|\(#'
But I will get and empty space.
result:
Array ( [0] => 'Abd Al-Hamid [1] => Ja'far [2] => A Two Hour Delay [3] => 2001) )
Array ( [0] => 't Hoen [1] => Frans [2] => [3] => De reünie [4] => 1963) [5] => TV) )
As for the 1st expression I used another pattern but I still get empty spaces. Any ideas?
EDIT:
Thanks this helped indeed. I tried using a modified version on the first string:
$pattern4 = '#[",\t]+|[{}]+|[()]+#';
However I still get an empty space:
Array ( [0] => Balko [1] => Vlado [2] => Panelák [3] => [4] => 2008 [5] => [6] => Byt na tretom [7] => #1.55 [8] => [9] => )
What should I do? I think that the " and the brackets are causing the problem but I don't know how to fix it.
I would surmise you have two tabs as separator in your second and third example string. (Can't see that here, the SO editor converts them into spaces).
But you could adapt your regex slightly in that case:
$pattern = '#,|\t+|\(#'
Or simpler even:
$pattern = '#[,\t(]+#'
And the alternatve, btw, would be just applying array_filter() on the result arrays to remove the empty entries.
Related
Hello, I need a array with all the words from a Text, ignoring the signs !&,. and everything that the {L} already do - except ' because it is used by some words like "can't". This is what I came so far:
$Text = "This is a Text! I can't understand any of these 11 words, by the way.";
$criteria="(\P{L}+|')";
$Text= preg_split($criteria, $Text);
Output
Array: ['This','is','a','Text','I','can','t','understand','any','of','these','words','by','the','way']
the point is that I want to consider can't as a word not to be split. How can I put this ' exception on it? Thanks in advance for your time and patience!**
may be you are looking for this.
<?php
$Text = "This is a Text! I can't understand any of these 11 words, by the way.";
$criteria="/[\s.!,]+/";
$Text= preg_split($criteria, $Text);
print_r($Text);
?>
Just put the symbol that you want to be ignored in big brackets.
Here \s is for space and tabs to ignore
Output
Array ( [0] => This [1] => is [2] => a [3] => Text [4] => I [5] => can't [6] => understand [7] => any [8] => of [9] => these [10] => 11 [11] => words [12] => by [13] => the [14] => way [15] => )
I have this string/content:
#Salome, #Jessi H and #O'Ren were playing at the #Lean's yard with "#Ziggy" the mouse.
Well, I am trying to get all names focuses above. I have used # symbol to create like a hash to be used in my web. If you note, there are names with spaces between like #Jessi H and characters before and after like #Ziggy. So, I don't my if you suggest me another way to manage the hash in another way to get it works correctly. I was thinking that for user that have white spaces, could write the hash with quotes like #"Jessi H". What do you think? Other examples:
#Lean's => #"Lean"'s
#Jessi H => #"Jessi H"
"#Jessi H" => (sorry, I don't know how to parse it)
#O'Ren => #"O'Ren"
What I have do?
I'm starting using regex in php, but some SO questions have been usefull for me to get started, so, these are my tries using preg_match_all function firstly:
Result of /#(.*?)[,\" ]/:
Array ( [0] => Salome [1] => Jessi [2] => Charlie [3] => Lean's [4] => Ziggy" ) )
Result of /#"(.*?)"/ for names like #"name":
Empty array
Guys, I don't expect that you do it all for me. I think that a pseudo-code or something like this will be helpful to guide me to the right direction.
Try the following regex: '/#(?:"([^"]+)|([^\b]+?))\b/'
This will return two match groups, the first containing any quoted names (eg #"Jessi H" and #"O'Ren"), and the second containing any unquoted names (eg #Salome, #Leon)
$matches = array();
preg_match_all('/#(?:"([^"]+)|([^\b]+?))\b/', '#Salome, #"Jessi H" and #"O\'Ren" were playing at the #Lean\'s yard with "#Ziggy" the mouse.', $matches);
print_r($matches);
Output:
Array
(
[0] => Array
(
[0] => #Salome
[1] => #"Jessi H
[2] => #"O'Ren
[3] => #Lean
[4] => #Ziggy
)
[1] => Array
(
[0] =>
[1] => Jessi H
[2] => O'Ren
[3] =>
[4] =>
)
[2] => Array
(
[0] => Salome
[1] =>
[2] =>
[3] => Lean
[4] => Ziggy
)
)
Are you setting these requirements or can you choose them? If you can set the requirements, I would suggest using _ instead of spaces, which would allow you to use the regex:
/#(.+) /
If spaces must be allowed and you're going with quotes, then the quotes should probably span the entire name, allowing for this regex:
/#\"(.+)\" /
I am using glob() php function to find the files in a directory.
I am getting the following array as result:
Array
(
[0] => D:\xampp\htdocs\myproject\app\webroot\batch\temp\file_2012-08-09-0.jpeg
[1] => D:\xampp\htdocs\myproject\app\webroot\batch\temp\file_2012-08-09-1.jpeg
[2] => D:\xampp\htdocs\myproject\app\webroot\batch\temp\file_2012-08-09-10.jpeg
[3] => D:\xampp\htdocs\myproject\app\webroot\batch\temp\file_2012-08-09-11.jpeg
[4] => D:\xampp\htdocs\myproject\app\webroot\batch\temp\file_2012-08-09-2.jpeg
[5] => D:\xampp\htdocs\myproject\app\webroot\batch\temp\file_2012-08-09-3.jpeg
[6] => D:\xampp\htdocs\myproject\app\webroot\batch\temp\file_2012-08-09-4.jpeg
[7] => D:\xampp\htdocs\myproject\app\webroot\batch\temp\file_2012-08-09-5.jpeg
[8] => D:\xampp\htdocs\myproject\app\webroot\batch\temp\file_2012-08-09-6.jpeg
[9] => D:\xampp\htdocs\myproject\app\webroot\batch\temp\file_2012-08-09-7.jpeg
[10] => D:\xampp\htdocs\myproject\app\webroot\batch\temp\file_2012-08-09-8.jpeg
[11] => D:\xampp\htdocs\myproject\app\webroot\batch\temp\file_2012-08-09-9.jpeg
)
What I want is "To sort the files on the basis of partial text of file names". As in the above array, when you may realize I was supposed to expect the result to be sorted as the ...-0.jpeg, ...-1.jpeg, ...-2.jpeg not ...-0.jpeg, ...-1.jpeg, ...-10.jpeg.
I am expecting the following result:
Array
(
[0] => D:\xampp\htdocs\myproject\app\webroot\batch\temp\file_2012-08-09-0.jpeg
[1] => D:\xampp\htdocs\myproject\app\webroot\batch\temp\file_2012-08-09-1.jpeg
[2] => D:\xampp\htdocs\myproject\app\webroot\batch\temp\file_2012-08-09-2.jpeg
[3] => D:\xampp\htdocs\myproject\app\webroot\batch\temp\file_2012-08-09-3.jpeg
[4] => D:\xampp\htdocs\myproject\app\webroot\batch\temp\file_2012-08-09-4.jpeg
[5] => D:\xampp\htdocs\myproject\app\webroot\batch\temp\file_2012-08-09-6.jpeg
[7] => D:\xampp\htdocs\myproject\app\webroot\batch\temp\file_2012-08-09-7.jpeg
[8] => D:\xampp\htdocs\myproject\app\webroot\batch\temp\file_2012-08-09-8.jpeg
[9] => D:\xampp\htdocs\myproject\app\webroot\batch\temp\file_2012-08-09-9.jpeg
[10] => D:\xampp\htdocs\myproject\app\webroot\batch\temp\file_2012-08-09-10.jpeg
[11] => D:\xampp\htdocs\myproject\app\webroot\batch\temp\file_2012-08-09-11.jpeg
)
Kindly help, how I can do it using PHP with/without regular expression?
Use natsort function that sort an array using a "natural order" algorithm
As you have all strings in an array, simply use php's built-in function sort().
It would work like this:
sort($array);
Since all files are in the same directory, this should solve the incorrect order you have. If it doesn't, you can also try this:
//Get rid of directory
foreach($array as $key=>$value){
$value=str_replace('D:\xampp\htdocs\myproject\app\webroot\batch\temp\\','',$value); //Watch out, the last backslash has to be escaped with another backslash, otherwise the string will not end
}
//Sort only the filenames
$sort($array);
//Prepend directory again
foreach($array as $key=>$value){
$value='D:\xampp\htdocs\myproject\app\webroot\batch\temp\\'.$value;
}
The best way to explain my problem is to just show you.
Input String:
/04-11-2010/12:45/
Regular Expression to get date and time parts:
preg_match('#/(\d\d)-(\d\d)-(\d\d\d\d)/(\d\d):(\d\d)/#', $input, $matches);
PHP Matches Array:
Array
(
[0] => /01-11-2010/12:45/
[1] => 01
[2] => 11
[3] => 2010
[4] => 12
[5] => 45
)
Now the above regex works perfectly at getting the individual component parts that represent the date and time in the input string.
The problem is that the time part needs to be optional without bringing down the entire regular expression.
Problem Input String:
/04-11-2010//
PHP Matches Array
Array
(
)
Basically what I need to be returned by the matches array is:
Array
(
[0] => /01-11-2010/12:45/
[1] => 01
[2] => 11
[3] => 2010
[4] =>
[5] =>
)
Note array elements 4 and 5 still need to exist but return empty.
Use the question mark operator and a non-capturing group to make stuff optional.
#/(\d\d)-(\d\d)-(\d\d\d\d)/(?:(\d\d):(\d\d))?/#
I'm not sure how this interacts with the match array - if having the empty array elements is absolutely critical, you might need to instead go for
#/(\d\d)-(\d\d)-(\d\d\d\d)/((?:\d\d)?):?((?:\d\d)?)/#
Which has its own false-positives (the colon in the time is now optional).
Make the second part optional:
'#/(\d\d)-(\d\d)-(\d\d\d\d)/(?:(\d\d):(\d\d))?/#'
Here a non-capturing group (?:…) is used that cannot be referenced and thus doesn’t change the matching groups.
#/(\d\d)-(\d\d)-(\d\d\d\d)/((?:\d\d)?):?((?:\d\d)?)/#
does what you want (i.e. populates groups 4 and 5), but also accepts incomplete times like in
/04-11-2010/12:/
don't know if this is fine with you
I'm not a php-head, but how about:
preg_match('#/(\d\d)-(\d\d)-(\d\d\d\d)/(\d\d)?:?(\d\d)?/#', $input, $matches);
As far as regexps go, that should match a string that has no time field.
#OP, don't need messy regex.
$str="/04-11-2010/12:45/";
$s = array_filter(explode('/',$str));
$date=$s[1];
$time=$s[2];
$date_parts=explode("-",$date);
$time_parts=explode(":",$time);
if ( checkdate($date_parts[1],$date_parts[0],$date_parts[2]) ){
print "date ok\n";
}
Use native PHP functions for this task, using regular expressions is a bit of an overkill.
PHP 5 has the date_parse function:
$string = '/04-11-2010/12:45/';
$dateArray = date_parse(str_replace('/', ' ', $string));
print_r($dateArray);
$string = '/04-11-2010//';
$dateArray = date_parse(str_replace('/', ' ', $string));
print_r($dateArray);
Output:
Array
(
[year] => 2010
[month] => 11
[day] => 4
[hour] => 12
[minute] => 45
[second] => 0
[fraction] => 0
[warning_count] => 0
[warnings] => Array
(
)
[error_count] => 0
[errors] => Array
(
)
[is_localtime] =>
)
Array
(
[year] => 2010
[month] => 11
[day] => 4
[hour] =>
[minute] =>
[second] =>
[fraction] =>
[warning_count] => 0
[warnings] => Array
(
)
[error_count] => 0
[errors] => Array
(
)
[is_localtime] =>
)
PHP 5.3 has a more flexible date_parse_from_format function that you could also use.
Not sure how to go about this...
But, I have two arrays, one with updated information, another with outdated information... There are a lot more elements in the second array, but I'm looking to "update" the outdated one with the updated information.
Here's what the arrays look like:
//Outdated
Array (
[0] => Array
(
[anum] => 3236468462
[cid] => 4899097762
[mid] => 1104881401
[na_title] =>
[na_fname] => JOHN
[m_initial] =>
[na_lname] => DOE
[na_suffix] =>
[na_addr1] => 1234 SAMPLE AVENUE
[na_addr2] =>
[na_city] => NORWALK
[state] => OH
[zip] =>
[zip_plus_4] =>
[route] => R002
[dma_code] => 510334
)
)
//Updated
Array (
[1] => Array
(
[0] => YUD990
[1] => 98
[2] => 1234 Sample Avenue
[3] =>
[4] => Norwalk
[5] => OH
[6] => 44857-9215
[7] => 3236468462
)
)
To clarify, I want to:
(1) Match up the value for [7] from the updated array with the value for [anum] in the outdated array, and then update [na_addr1], [na_addr2], [na_city], [state], [zip], [zip_plus_4] in the outdated array with the values for [2],[3],[4],[5],[6] (I know I'll need to split the updated [6] in order to get it to map corrected to the outdated)
Feel like I'm making this very confusing... sorry about that...
Your updated array needs to have matching keys, otherwise there's no way to know how the values should replace the old ones. Then the standard array merge works.
$new = array_merge($outdated, $updated);
Assuming that the structure of the update array will never change, you could just use the code below. (I'm assuming that 'zip' is the first 5 digits and zip_plus_4 is the last 4 digits, but I'm not clear exactly what they're supposed to be.)
$old_array['anum'] = $new_array[7];
$old_array['na_addr1'] = $new_array[2];
$old_array['na_addr2'] = $new_array[2];
$old_array['na_city'] = $new_array[3];
$old_array['state'] = $new_array[4];
$zip_code = explode('-', $new_array[6]);
$old_array['zip'] = $zip_code[0];
$old_array['zip_plus_4'] = $zip_code[1];
I'm not sure why the second array doesn't use its own set of matching keys. It would be more readable and help keeps things consistent. For example, if you end up adding another field, some of the elements could be offset by one, which would just cause headaches. But if the arrays used the same keys, you could use the code below and everything would be fine.
$old_array['anum'] = $new_array['anum'];
$old_array['na_addr1'] = $new_array['na_addr1'];
(etc)