I have the following string in PHP.
"File:content/resources/11933/Calm_Nebula_by_The_Free_Mason.jpgDate:Fri Mar 18 11:30:17 CET 2016Size:23124"
I need to fetch file, date, and size separately.
What is the best way to extract the required values?
explode() OR substring()
Explode requires some common value, substring requires start and end. In both cases i am a bit confuse.
Any help would be greatly appreciated.
Try this:
<?php
$text = "File:content/resources/11933/Calm_Nebula_by_The_Free_Mason.jpgDate:Fri Mar 18 11:30:17 CET 2016Size:23124";
$pattern = '/file:(.*?)date:(.*?)size:(.*)/i';
preg_match($pattern, $text, $out);
print_r($out);
?>
Output:
Array
(
[0] => File:content/resources/11933/Calm_Nebula_by_The_Free_Mason.jpgDate:Fri Mar 18 11:30:17 CET 2016Size:23124
[1] => content/resources/11933/Calm_Nebula_by_The_Free_Mason.jpg
[2] => Fri Mar 18 11:30:17 CET 2016
[3] => 23124
)
Demo: https://3v4l.org/OcSXH
Related
i want to see all phone numbers in my string. right now it has only one number in the array 'match' how can i get all the numbers in my array?
$str = "djvdsfhis 0647382938 rdfrgfdg tel:0647382938 rfgdfgfd 06 47 38 29
38 fdgdfrggfd tel:06-47382938 cxgvfdgsfdc";
$arr = '~\d{2}-\d{8}|\d{10}~';
$success = preg_match($arr, $str, $match);
if ($success) {
echo "Match: ".$match[0]."<br />";
print_r($match);
}
i get this as output:
djvdsfhis ffgfg 0647382938 rdfrgfdg tel:0647382938 rfgdfgfd 06 47 38 29 38 fdgdfrggfd tel:06-47382938 cxgvfdgsfdc
Match: 0647382938
Array ( [0] => 0647382938 )
but i want to have my array like this:
Array ( [0] => 0647382938 [1] => 0647382938 [2] => 06-47382938
You should use preg_match_all. Which will output an array of all of the results of your regex, in this instance an array of the numbers.
$str = "djvdsfhis 0647382938 rdfrgfdg tel:0647382938 rfgdfgfd 06 47 38 29
38 fdgdfrggfd tel:06-47382938 cxgvfdgsfdc";
$arr = '~\d{2}-\d{8}|\d{10}~';
$success = preg_match_all($arr, $str, $match);
if ($success) {
print_r($match);
}
Test it here :
http://sandbox.onlinephpfunctions.com/code/350d10b1be46ce3a5851d7671750bac28f9110f0
You can also use T-Regx tool which has automatic delimiters:
pattern('\d{2}-?\d{8}')->match($str)->all();
I have a string that should contain a name and a date of registration:
Dipak Misra - 18 Nov 2018 10:20
I want to convert this string into array like this:
Array ( [0] => Dipak Misra [1] => 18 Nov 2018 [2] => 10:20 )
I am using preg_match_all() function:
$re = '/\w+(?:[-\s]\w+)*/';
$str = "Dipak Misra - 18 Nov 2018 10:20";
preg_match_all($re, $str, $matches);
print_r($matches[0]);
I am getting output Like this:
Array ( [0] => Dipak Misra [1] => 18 Nov 2018 10 [2] => 20 )
Please guide me.
If the format of that string does not change, you could split on either a space, hyphen space, or split on a space where on the left and the right side a digit is present instead of matching.
- |(?<=\d) (?=\d)
Regex demo
For example:
print_r(preg_split("/ - |(?<=\d) (?=\d)/", "Dipak Misra - 18 Nov 2018 10:20"));
Result:
Array
(
[0] => Dipak Misra
[1] => 18 Nov 2018
[2] => 10:20
)
Use preg_match() with ([^-]+)\s+-\s+(.*)\s(\d+:\d+) as pattern instead
$re = '/([^-]+)\s+-\s+(.*)\s(\d+:\d+)/';
$str = "Dipak Misra - 18 Nov 2018 10:20";
preg_match($re, $str, $matches);
unset($matches[0]);
print_r($matches);
Check result in demo
$pattern = "/(\w+\s\w+)\s-\s([0-9]{2}\s\w+\s[0-9]{4})\s([0-9]+:[0-9]+)/";
$chain = "Dipak Misra - 18 Nov 2018 10:20";
preg_match_all($pattern, $chain, $matches);
$data = [
'chain' => $chain,
'pattern' => $pattern,
'name' => $matches[1],
'date' => $matches[2],
'time' => $matches[3]
];
echo "<pre>";
print_r($data);
echo "<pre>";
the output
Please tell how to make regex code to find all occurrence of date from the string, in the format listed below :
26 jan 2016
26th jan 2016
26 january 2016
26th january 2016
26 feb 15
26th feb 15
ie :
$string = "Test 26 jan 2016 test test test 12 Feb 15 test test test 17 January 2013 nice testing 123 6 12 2016";
I want the result as an array where i will get :
$res = array (
'2016-01-26',
'2015-02-12',
'2013-01-27'
)
Please tell how to make the same using PHP regex.
The solution using preg_match_all, array_map, date and strtotime functions (I've modified the initial string a bit to get a complex case):
$string = "Test 26th jan 2016 test test test 12 Feb 15 test test test 17 January 2013 nice testing 123 6 12 2016";
preg_match_all("/\b\d{2}(?:\w{2})? \w+? \d{2,4}\b/i", $string, $matches);
$dates = array_map(function($d) {
return date('Y-m-d', strtotime($d));
}, $matches[0]);
print_r($dates);
The output:
Array
(
[0] => 2016-01-26
[1] => 2015-02-12
[2] => 2013-01-17
)
I would like to find the date in a string after a particular word (key).
My string is dynamic and the date format also not same from one string to another string.
$data = "Balance- 0.30,Val-Aug 29 2013, Free Bal- 0.00";
or
$data = "Bal: 96.27.Valid Sep 26 2013.Toll Free Dial 578785";
or
$data = "BalanceRs.0.00,Expiry date: Apr 04 20141 Live Scores";
or
$data = "Your current balance is 0.20.Your account expires on 2013-11-23 23:59:59.";
or
$data = "Main Bal Rs.87.850 Val 09-07-2014,More";
$key = array('Val-','Val','Valid','Expiry date:','expires on');
$result=preg_match_all("/(?<=(".$key."))(\s\w*)/i",$data,$networkID);
$myanswer = #$networkID[0][0];
Here I am getting the output of only the first word.
Anyone please guide me to get the date. Thanks.
How about:
$data = "Balance- 0.30,Val-Aug 29 2013, Free Bal- 0.00";
$data .= "Bal: 96.27.Valid Sep 26 2013.Toll Free Dial 578785";
$data .= "BalanceRs.0.00,Expiry date: Apr 04 20141 Live Scores";
$data .= "Your current balance is 0.20.Your account expires on 2013-11-23 23:59:59.";
$data .= "Main Bal Rs.87.850 Val 09-07-2014,More";
$key = array('Valid','Val-','Val','Expiry date:','expires on');
$key_str = implode('|', $key);
preg_match_all("/(?<=$key_str)\s*((?:\w{3} \d\d \d{4})|(?:\d{4}-\d\d-\d\d)|(?:\d\d-\d\d-\d{4}))/i", $data, $networkID);
print_r($networkID);
output:
Array
(
[0] => Array
(
[0] => Aug 29 2013
[1] => Sep 26 2013
[2] => Apr 04 2014
[3] => 2013-11-23
[4] => 09-07-2014
)
[1] => Array
(
[0] => Aug 29 2013
[1] => Sep 26 2013
[2] => Apr 04 2014
[3] => 2013-11-23
[4] => 09-07-2014
)
)
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/"