regex/php: parse times from string? - php

What is the best way to parse time from a string?
Example string: "this is 10:45 this is 10:48, 10:49, 10:50, 22:15";
Expected return:
[0] = 10:45
[1] = 10:48
[2] = 10:49
[3] = 10:50
[4] = 22:15
Thanks for any replies!!

This will give the output you want and limits your hour/minute numbers to valid values for the first position of the hour and the first position of the minute:
$y = "this is 10:45 this is 10:48, 10:49, 10:50, 22:15";
preg_match_all("/(([0-1]\d|2[0-3]):[0-5]\d)/",$y,$matches);
print_r($matches[1]);
/*
Outputs:
Array ( [0] => 10:45 [1] => 10:48 [2] => 10:49 [3] => 10:50 [4] => 22:15 )
/*

preg_match_all('/\d{2}:\d{2}/', $string, $matches);
You will have all your matches in $matches array.

That looks like a pretty simple case. try preg_match_all with "/\d\d:\d\d/"

Related

php split / cluster binary into chunks based on next 1 in cycle

I need to figure out a method using PHP to chunk the 1's and 0's into sections.
1001 would look like: array(100,1)
1001110110010011 would look like: array(100,1,1,10,1,100,100,1,1)
It gets different when the sequence starts with 0's... I would like it to segment the first 0's into their own blocks until the first 1 is reached)
00110110 would look like (0,0,1,10,1,10)
How would this be done with PHP?
You can use preg_match_all to split your string, using the following regex:
10*|0
This matches either a 1 followed by some number of 0s, or a 0. Since a regex always tries to match the parts of an alternation in the order they occur, the second part will only match 0s that are not preceded by a 1, that is those at the start of the string. PHP usage:
$beatstr = '1001110110010011';
preg_match_all('/10*|0/', $beatstr, $m);
print_r($m);
$beatstr = '00110110';
preg_match_all('/10*|0/', $beatstr, $m);
print_r($m);
Output:
Array
(
[0] => Array
(
[0] => 100
[1] => 1
[2] => 1
[3] => 10
[4] => 1
[5] => 100
[6] => 100
[7] => 1
[8] => 1
)
)
Array
(
[0] => Array
(
[0] => 0
[1] => 0
[2] => 1
[3] => 10
[4] => 1
[5] => 10
)
)
Demo on 3v4l.org

conversion from gis to lat/long give me big trouble in charset

i've a string like
$input="16°28'60,00''"
thats is on my db and stored as TEXT utf8_general_ci
im trying to convert it to decimal/lat-long system. So I write a function that splice the input and convert it.
Im using $input as an array, and when is on position 2, I have a strange result thats broke my function:
$input[2]---> 'b"Â"'
in position 2 there is the "°"
the next row check if esist "°" but due this error can works
if($tempD == iconv("UTF-8", "ISO-8859-1//TRANSLIT", '°')
how can i fix that?
If the format of the DB string is always the same, just grab the digits out and you don't need to bother with the degrees, minutes, seconds.
$input = "16°28'60,00''";
preg_match_all("/(\d+)/", $input, $match);
print_r($match);
Output:
Array
(
[0] => Array
(
[0] => 16
[1] => 28
[2] => 60
[3] => 00
)
[1] => Array
(
[0] => 16
[1] => 28
[2] => 60
[3] => 00
)
)
Now you have each digit and you can convert it easily.

How to split a string in multiple ones (Php)?

I want to split a big number/string for example 123456789123456789 into 6 smaller strings/numbers of 3 characters each. So the result would be 123 456 789 123 456 789. How can I do this?
Use chunk_split():
$var = "123456789123456789";
$split_string = chunk_split($var, 3); // 3 is the length of each chunk
If you want your result as an array, you can use str_split():
$var = "123456789123456789";
$array = str_split($var, 3); // 3 is the length of each chunk
You may use chunk_split() function.
It splits a string into smaller
$string = "123456789123456789";
echo chunk_split ($string, 3, " ");
will output
123 456 789 123 456 789
First parameter is the string to be chunked. The second is the chunk length and the third is what you want at the end of each chunk.
See PHP manual for further information
You could do something like this:
$string = '123456789123456789';
preg_match_all('/(\d{3})/', $string, $matches);
print_r($matches[1]);
Output:
Array
(
[0] => 123
[1] => 456
[2] => 789
[3] => 123
[4] => 456
[5] => 789
)
\d is a number and {3} is 3 of the previously found character (in this case a number.
....
or if there won't always be even groupings:
$string = '12345678912345678922';
preg_match_all('/(\d{1,3})/', $string, $matches);
print_r($matches[1]);
Output:
Array
(
[0] => 123
[1] => 456
[2] => 789
[3] => 123
[4] => 456
[5] => 789
[6] => 22
)
Demo: https://regex101.com/r/rX0pJ1/1

Use RegEx to get (parsed) times from string

I need some direction on how to alter my current RegEx line.
Example string:
9:00 AM - 9:30 AM
Desired output:
900 930 (each can be an index in an array or whatever)..
Current approach:
preg_match('/^([0-9]{1,2}):([0-9]{1,2}) ?([ap]m)/i', trim($presentationtime), $matches);
however this only seems to get me the FIRST (stripped/parsed) time.
results:
$presentationtime = $matches[1] . $matches[2];
echo 'Matches check: '. $matches[0] . '<br>';
echo 'Matches check: '. $matches[1] . '<br>';
returns:
Matches check: 9
Matches check: 00
How can I alter my regex to get BOTH times (stripped/parsed the same way)..
I'm expecting a 6 index array..but can only get a 3 index/count array
as #anubhava says use preg_match_all
$presentationtime = '9:00 AM - 9:30 AM';
preg_match_all('/([0-9]{1,2}):([0-9]{1,2}) /', trim($presentationtime), $matches);
print_r($matches);
results:
Array
(
[0] => Array
(
[0] => 9:00
[1] => 9:30
)
[1] => Array
(
[0] => 9
[1] => 9
)
[2] => Array
(
[0] => 00
[1] => 30
)
)
Edit to answer the comment:
very lazy workaround to get one dimensional array, banal regex
$presentationtime = '9:00 AM - 9:30 PM';
preg_match('/([0-9]{1,2}):([0-9]{1,2})\s([apm]+)\s-\s([0-9]{1,2}):([0-9]{1,2})\s([apm]+)/i', trim($presentationtime), $matches);
result
Array
(
[0] => 9:00 AM - 9:30 PM
[1] => 9
[2] => 00
[3] => AM
[4] => 9
[5] => 30
[6] => PM
)
You have to simply use preg_match_all (as stated by anubhava in the comment) and remove the ^ at the start of the regex:
preg_match_all('/([0-9]{1,2}):([0-9]{1,2}) ?([ap]m)/i', trim($presentationtime), $matches);
Then $matches will be like:
Array
(
[0] => Array
(
[0] => 9:00 AM
[1] => 9:30 AM
)
[1] => Array
(
[0] => 9
[1] => 9
)
[2] => Array
(
[0] => 00
[1] => 30
)
[3] => Array
(
[0] => AM
[1] => AM
)
)
If you want to refine the regex you may use:
preg_match('/([0-9]{1,2}):([0-5]\d)\s*([ap]m)/i', trim($presentationtime), $matches);
The minute section is will match 2 digits from 00 to 59, the space section \s* is optional and matches more than one whitespace char (space, tab, CR, FF...)
One way of doing that:
$data = '9:00 AM - 9:30 AM';
print str_replace('-', ' ', preg_replace("/[^0-9-]/","", $data));
Result:
900 930
keep digits and dash
replace dash with space
In the previous example, I was using limited regex with preg_replace. To keep things simple and using time with AM/PM, I'd just explode (as you already thought about),
$data = '9:00 AM - 9:30 PM';
$timeArray = array_map('trim', explode('-', $data)); // trim each array item
$timeArray = array_map('Time24Hr', $timeArray); // clean up time
print_r($timeArray);
function Time24Hr($time) {
$time_ampm = explode(' ', $time);
if (strtolower(trim($time_ampm[1])) === 'am') {
// A
return $time_ampm[0];
// B
//return str_replace(':', '', $time_ampm[0]);
// C
//return $time;
}
$hr_min = explode(':', trim($time_ampm[0]));
// A
return ($hr_min[0] + 12) . ':' . $hr_min[1];
// B
//return ($hr_min[0] + 12) . $hr_min[1];
// C
//return ($hr_min[0] + 12) . ':' . $hr_min[1]. ' ' . $time_ampm[1];
}
Results:
Result A:
Array
(
[0] => 9:00
[1] => 21:30
)
Result B:
Array
(
[0] => 900
[1] => 2130
)
Result C: -- obviously this is insane, but you can easily make it sane. This is just an example
Array
(
[0] => 9:00 AM
[1] => 21:30 PM
)
Of course, this simplistic method can be streamlined much further. This method doesn't need regex.

Using regular expressions to match a time interval

I'm having problems on how to preg_match this time statement.
TF 02:30 pm-04:00 am
I was able to separate the time into the array but I also want to get the AM and PM as well as the letter T and F.
This is for a class schedule module that I am working on. The data I got from the database is that string. I want to separate them so that I can manipulate the entries for the calendar that I have.
Here's what I have at this point.
$sampleString = 'T 02:30 pm-04:00 am';
$pattern = '/([0-1]?\d|2[0-9]):([0-5]?\d)/';
preg_match_all($pattern,$sampleString,$time);
print_r($time);
The output:
Array (
[0] => Array (
[0] => 02:30
[1] => 04:00 )
[1] => Array (
[0] => 02
[1] => 04 )
[2] => Array (
[0] => 30
[1] => 00 )
)
Thanks.
As recommended by IMSoP, splitting this up into parts makes it easier (looking again, I think your hour regex could use improvement, as it will accept hours from 0-29, I've changed it to 0?[1-9]|1[0-2] instead, to accept only 1 - 12)
Days: [MTWHFS]+
Space: \s
Hour: 0?[1-9]|1[0-2]
Colon: :
Minute: [0-5]?\d
Space: \s
am/pm: [ap]m
hyphen: -
Hour: 0?[1-9]|1[0-2]
Colon: :
Minute: [0-5]?\d
Space: \s
am/pm: [ap]m
Then just put them together, surrounding the desired capturing groups with parentheses:
([MTWHFS]+)\s(0?[1-9]|1[0-2]):([0-5]?\d)\s([pa]m)-(0?[1-9]|1[0-2]):([0-5]?\d)\s([pa]m)

Categories