I'm working on some data loaded from an Excel file using PHPExcel. Everything is working fine with every fields but I'm having some troubles with a field which should contain a pattern.
The pattern is
(([0-2]?[0-9]\:[0-5]?[0-9]\:([0-5]?[0-9])\/([0-2]?[0-9]\:[0-5]?[0-9]\:([0-5]?[0-9])))\s?)*
that should check for time intervals such as "00:30:45/01:40:12 01:10:34/1:07:12" and so on.
My problem is that preg_match fails to check this pattern against the sting "1:00". If I add a variable to store the matches returns an one element array with the value "".
I check the pattern with an online tool (http://regexpal.com/) and work as expected but for an to me unknown reason, preg_match doesn't.
Is there something I am missing?
try this
<?php
$string = "00:30:45/01:40:12 01:10:34/1:07:12";
preg_match_all('!((\d{1,2}:\d{1,2}:\d{1,2}/(\d{1,2}:\d{1,2}:(\d{1,2}))))\s?!', $string, $matches);
echo "<pre>";
print_r($matches);
echo "</pre>";
you need to get this:
array (
0 =>
array (
0 => '00:30:45/01:40:12 ',
1 => '01:10:34/1:07:12',
),
1 =>
array (
0 => '00:30:45/01:40:12',
1 => '01:10:34/1:07:12',
),
2 =>
array (
0 => '00:30:45/01:40:12',
1 => '01:10:34/1:07:12',
),
3 =>
array (
0 => '01:40:12',
1 => '1:07:12',
),
4 =>
array (
0 => '12',
1 => '12',
),
)
demo
<?php
$str = '00:30:45/01:40:12 01:10:34/1:07:12/1:00';
//$str = '1:00';
$p = '/[0-9]+:[0-9]+(:[0-9]+|)/s';
preg_match_all($p, $str, $m);
print_r($m);
outputs
Array
(
[0] => Array
(
[0] => 00:30:45
[1] => 01:40:12
[2] => 01:10:34
[3] => 1:07:12
[4] => 1:00
)
[1] => Array
(
[0] => :45
[1] => :12
[2] => :34
[3] => :12
[4] =>
)
)
I finally managed to fix this up. After taking a break and come again to the code with a clear mind, I noticed the final '*' should by a '+' in order to force to have, at least, one instance of the time interval.
Thank you all for your responses.
Related
I'm trying to find a regex capable of capturing the content of short codes produces in Wordpress.
My short codes have the following structure:
[shortcode name param1="value1" param2="value2" param3="value3"]
The number of parameters is variable.
I need to capture the shortcode name, the parameter name and its value.
The closest results I have achieved is with this:
/(?:\[(.*?)|\G(?!^))(?=[^][]*])\h+([^\s=]+)="([^\s"]+)"/
If I have the following content in the same string:
[specs product="test" category="body"]
[pricelist keyword="216"]
[specs product="test2" category="network"]
I get this:
0=>array(
0=>[specs product="test"
1=> category="body"
2=>[pricelist keyword="216"
3=>[specs product="test2"
4=> category="network")
1=>array(
0=>specs
1=>
2=>pricelist
3=>specs
4=>)
2=>array(
0=>product
1=>category
2=>keyword
3=>product
4=>category)
3=>array(
0=>test
1=>body
2=>216
3=>test2
4=>network)
)
I have tried different regex models but I always end up with the same issue, if I have more than one parameter, it fails to detect it.
Do you have any idea of how I could achieve this?
Thanks
Laurent
You could make use of the \G anchor using 3 capture groups, where capture group 1 is the name of the shortcode, and group 2 and 3 the key value pairs.
Then you can remove the first entry of the array, and remove the empty entries in the 1st, 2nd and 3rd entry.
This is a slightly updated pattern
(?:\[(?=[^][]*])(\w+)|\G(?!^))\h+(\w+)="([^"]+)"
Regex demo | Php demo
Example
$s = '[specs product="test" category="body"]';
$pattern = '/(?:\[(?=[^][]*])(\w+)|\G(?!^))\h+(\w+)="([^"]+)"/';
$strings = [
'[specs product="test" category="body"]',
'[pricelist keyword="216"]',
'[specs product="test2" category="network" key="value"]'
];
foreach($strings as $s) {
if (preg_match_all($pattern, $s, $matches)) {
unset($matches[0]);
$matches = array_map('array_filter', $matches);
print_r($matches);
}
}
Output
Array
(
[1] => Array
(
[0] => specs
)
[2] => Array
(
[0] => product
[1] => category
)
[3] => Array
(
[0] => test
[1] => body
)
)
Array
(
[1] => Array
(
[0] => pricelist
)
[2] => Array
(
[0] => keyword
)
[3] => Array
(
[0] => 216
)
)
Array
(
[1] => Array
(
[0] => specs
)
[2] => Array
(
[0] => product
[1] => category
[2] => key
)
[3] => Array
(
[0] => test2
[1] => network
[2] => value
)
)
I have a long string like this I1:1;I2:2;I8:2;NA1:5;IA1:[1,2,3,4,5];S1:asadada;SA1:[1,2,3,4,5];SA1:[1,2,3,4,5];. Now I just want to get certain words like 'I1','I2','I8','NA1' and so on i.e. words between ':'&';' only ,and store them in array. How to do that efficiently?
I have already tried using preg_split() and it works but giving me wrong output. As shown below.
// $a is the string I want to extract words from
$str = preg_split("/[;:]/", $a);
print_r($str);
The output I am getting is this
Array
(
[0] => I8
[1] => 2
[2] => I1
[3] => 1
[4] => I2
[5] => 2
[6] => I3
[7] => 2
[8] => I4
[9] => 4
[10] =>
)
Array
(
[0] => NA1
[1] => 5
[2] =>
)
Array
(
[0] => IA1
[1] => [1,2,3,4,5]
[2] =>
)
Array
(
[0] => S1
[1] => asadada
[2] =>
)
Array
(
[0] => SA1
[1] => [1,2,3,4,5]
[2] =>
)
But I am expecting 'I8','I1','I2','I3','I4' also in seperated array with position [0]. Any help on how to do this.
You could try something like.
<?php
$str = 'I1:1;I2:2;I8:2;NA1:5;IA1:[1,2,3,4,5];S1:asadada;SA1:[1,2,3,4,5];SA1:[1,2,3,4,5];';
preg_match_all('/(?:^|[;:])(\w+)/', $str, $result);
print_r($result[1]); // Matches are here in $result[1]
You can perform a greedy match to match the items between ; and : using preg_match_all()
<?php
$str = 'I1:1;I2:2;I8:2;NA1:5;IA1:[1,2,3,4,5];S1:asadada;SA1:[1,2,3,4,5];SA1:[1,2,3,4,5];';
preg_match_all('/;(.+?)\:/',$str,$matches);
print_r($matches[1]);
Live Demo: https://3v4l.org/eBsod
One possible approach is using a combination of explode() and implode(). The result is returned as a string, but you can easily put it into an array for example.
<?php
$input = "I1:1;I2:2;I8:2;NA1:5;IA1:[1,2,3,4,5];S1:asadada;SA1:[1,2,3,4,5];SA1:[1,2,3,4,5];.";
$output = array();
$array = explode(";", $input);
foreach($array as $item) {
$output[] = explode(":", $item)[0];
}
echo implode(",", $output);
?>
Output:
I1,I2,I8,NA1,IA1,S1,SA1,SA1,.
I'm facing a problem that I can't solve on my own, I can't find a way to do it.
I have those two strings:
'Germany, "Sightseeing, Travelling, Hotels"'
and
'Health, Medicin, Healthcare'
I need to explode() this strings on the , char, but only if the text, where this , lays in isn't surrounded by ".
So, as example this would be the desired results:
array(0 => 'Germany', 1 => '"Sightseeing, Travelling, Hotels"');
array(0 => 'Health', 1 => 'Medicin', 2 => 'Healthcare');
By now this is what I have:
explode(",", 'Germany, "Sightseeing, Travelling, Hotels"');
Which will give out
array(0 => 'Germany', 1 => '"Sightseeing', 2 => 'Travelling', 3 => 'Hotels"');
How can I get to this result?
See str_getcsv() how to parse csv (even if you are not reading a csv file, this is exactly what this string looks like):
<?php
print_r(str_getcsv('Germany, "Sightseeing, Travelling, Hotels"', ','));
print_r(str_getcsv('Health, Medicin, Healthcare'));
print_r(str_getcsv('D"uh!, \"Test\"', ','));
?>
results in:
Array ( [0] => Germany [1] => Sightseeing, Travelling, Hotels )
Array ( [0] => Health [1] => Medicin [2] => Healthcare )
Array ( [0] => D"uh! [1] => "Test" )
Remarks: This Function is available beginning with php version 5.3.0 and can also be configured as to what your input looks like. I added some special cases, this is a classic problem that looks far simpler than it is.
You can use php function str_getcsv.
Here an example:
$string_1 = 'Germany, "Sightseeing, Travelling, Hotels"';
$string_2 = 'Health, Medicin, Healthcare';
$result_1 = str_getcsv($string_1,',','"');
$result_2 = str_getcsv($string_2,',','"');
print_R($result_1);
print_R($result_2);
that outputs:
Array
(
[0] => Germany
[1] => Sightseeing, Travelling, Hotels
)
Array
(
[0] => Health
[1] => Medicin
[2] => Healthcare
)
You can after map each element of the resultant array, and if there is a comma you can surround it with "
foreach($result_1 as $i => $item) {
if(substr_count($item,',') > 0) {
$result_1[$i] = '"'.$item.'"';
}
}
Wich produce:
Array
(
[0] => Germany
[1] => "Sightseeing, Travelling, Hotels"
)
I have a string and I would like to explode with three differents patterns. The string looks like to :
country:00/00/00->link:00/00/00->link2
country2:00/00/00->link3:00/00/00->link4
I would like to get the differents parts of this two strings. The two lines are separated by a /n, the dates are separated by : and the link associated to date are separated with a ->
At the beginning I explode by the line break
$var = explode("\n", $var);
but when I tried to explode again this string, I get an error : *preg_split() expects parameter 2 to be string, array given*
How can I get the different parts ?
Thanks in advance.
Ideone link
Instead of using preg_split, consider using preg_match. You can write it as one big regex.
<?php
// Implicit newline. Adding \n would make an empty spot in the array
$str = "country:00/00/00->link:00/00/00->link2
country2:00/00/00->link3:00/00/00->link4";
$arr = split("\n", $str);
for ($i = 0; $i < count($arr); $i++) {
preg_match("/^(\w+)\:(\d\d\/\d\d\/\d\d)->(\w+)\:(\d\d\/\d\d\/\d\d)->(\w+)/", $arr[$i], $matches);
print_r($matches);
}
?>
Output:
Array
(
[0] => country:00/00/00->link:00/00/00->link2
[1] => country
[2] => 00/00/00
[3] => link
[4] => 00/00/00
[5] => link2
)
Array
(
[0] => country2:00/00/00->link3:00/00/00->link4
[1] => country2
[2] => 00/00/00
[3] => link3
[4] => 00/00/00
[5] => link4
)
EDIT
In your comment, you're posting dates with 4 digits, whereas in your question, they only had 2 digits.
Therefore you need to change the regex to:
/^(\w+)\:(\d\d\/\d\d\/\d\d\d\d)->(\w+)\:(\d\d\/\d\d\/\d\d\d\d)->(\w+)/
How about using preg_match_all:
<?php
$data =<<<ENDDATA
country:00/00/00->link:00/00/00->link2
country2:00/00/00->link3:00/00/00->link4
ENDDATA;
preg_match_all('#(\d{2}/\d{2}/\d{2})->(.[^:\n]+)#', $data, $matches);
print_r($matches);
Gives the following result:
Array
(
[0] => Array
(
[0] => 00/00/00->link
[1] => 00/00/00->link2
[2] => 00/00/00->link3
[3] => 00/00/00->link4
)
[1] => Array
(
[0] => 00/00/00
[1] => 00/00/00
[2] => 00/00/00
[3] => 00/00/00
)
[2] => Array
(
[0] => link
[1] => link2
[2] => link3
[3] => link4
)
)
your problem is that after using explode first time, it is turning into an array and explode function connat explode an array. You need to use a loop probablr for loop that targets array elemets then use explode function on those elements and you will have it.
See example Below:
<?php
$val="abc~~~def~~~ghi####jkl~~~mno~~~pqr###stu~~~vwx~~~yz1";
$val=explode("####", $val);
//result will be
$valWillBe=array(3) {
[0]=>'abc~~~def~~~ghi',
[1]=>'jkl~~~mno~~~pqr',
[2]=>'stu~~~vwx~~~yz1'
}
//if you want to explode again you use a loop
for($r=0; $r<sizeof($val); $r++){
$val[$r]=explode("~~~", $val[$r]);
}
//now you have your string exploded all in places.
?>
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.