Find date after a particular word - php

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
)
)

Related

Match hyphen character from a string to make an array output

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

using usort on simpleXML that's been converted to an array

I have read a standard RSS feed into simpleXML and get the following after converting to an array:
[0] => SimpleXMLElement Object (
[guid] => https://www.zazzle.com/scissors_by_any_other_name_dreadful_pun_t_shirt-235586301981812394
[pubDate] => Wed, 23 Aug 2017 13:41:08 GMT
[title] => SimpleXMLElement Object ( )
[link] => https://www.zazzle.com/scissors_by_any_other_name_dreadful_pun_t_shirt-235586301981812394
[author] => HightonRidley
[description] => SimpleXMLElement Object ( )
[price] => $30.15
)
[1] => SimpleXMLElement Object (
[guid] => {not enough reputation points to show link}
[pubDate] => Sat, 19 Aug 2017 15:53:19 GMT
[title] => SimpleXMLElement Object ( )
[link] => {not enough reputation points to show link}
[author] => HightonRidley
[description] => SimpleXMLElement Object ( )
[price] => $15.65 )
)
This is the code used to create and display the above
$sortable = array();
foreach($product_grid->channel->item as $node) {
$sortable[] = $node;
}
print_r($sortable);
To prove to myself it's working and accessible as intended I used this
foreach ($sortable as $rssitem) {
echo "<br>" . $rssitem->pubDate;
}
echo "<br>".$sortable[0]->pubDate;
echo "<br>".$sortable[1]->pubDate;
and got this output
Wed, 23 Aug 2017 13:41:08 GMT
Sat, 19 Aug 2017 15:53:19 GMT
Wed, 23 Aug 2017 13:41:08 GMT
Sat, 19 Aug 2017 15:53:19 GMT
...but when I try to sort using usort, I get no output and Firefox console tells me I have a server error 500
Here's how I'm using usort
usort($sortable, function($a, $b)
{
return strtotime($a->pubDate) > strtotime($b->pubDate);
});
I've checked various questions/answers on using usort here and as many other places as I can find online but to no avail.
Can someone please tell me how to reference the various elements of $sortable from within that anonymous function. I will be doing other sorts using other elements once I get this first one cracked.
Thanks!

Preg match to find all occurance of date in this format 26 jan 2016 or 26th jan 2016 or 26 january 2016

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
)

Fetch Date and a String from another string

I have a bunch of strings like this, for example:
Time: 22:30 (25) | Date: 19 March 2011 | Contributor: Salesman
Now, I want to extract the date and the string after Contributor, i.e, Salesman.
Currently, I am using explode() function in PHP.
But the problem is, the string has many variations like:
Time: 22:30 (25) | Date: 19 March 2011
Time: 22:30 (25) | Date: 2011 | Contributor: Salesman
Time: 22:30 (25) | Contributor: Salesman
Time: 22:30 (25) | Date: 2011
I want something that works perfect for all the variations. Where a field is unavailable, I shall consider it as NULL. For full date I need to store the date in database, and for only year I shall save the year.
Suggest me some code for this problem, or a regular expression in PHP if this problem can be solved through it.
Time:.*?\|\s*(?:Date:\s*([0-9a-zA-Z ]+))?\|?\s*(?:Contributor:\s*([a-zA-Z0-9 ]+))?
Try this.This will only give groups available.
See demo.
http://regex101.com/r/nG1gU7/18
You could try something like this:
$string = 'Time: 22:30 (25) | Date: 19 March 2011';
function str_func($string, $key, $remove_key = FALSE)
{
$string = trim($string);
if ($string[strlen($string) - 1] !== '|') {
$string .= '|';
}
$pos = strpos(strtolower($string), strtolower($key) . ':');
if ($pos !== FALSE) {
$return = strstr(substr($string, $pos), '|', true);
return ($remove_key) ? trim(substr($return, strlen($key) + 1)) : $return;
} else {
return NULL;
}
}
So just pass it the string and 'Time' or 'Contributor' (and optionally whether you'd like the key to be removed) e.g.
//Will return Time: 22:30 (25)
echo str_func($string, 'time');
//Will remove 'date: ' from the returned string
echo str_func($string, 'date', TRUE);
Hope this helps!
I think you want something like this,
^.*?\|\s*\K(?:Date:\s*((?:\d{2}\s*\S+)?\s*\d{4})\s*\|?)?\s*(?:Contributor:\s*(\S+))?
DEMO
Group index 1 contains the value of Date: field and group index 2 contains the value of Contributor: field.
Code:
<?php
$data = <<< EOT
Time: 22:30 (25) | Date: 19 March 2011
Time: 22:30 (25) | Date: 2011 | Contributor: Salesman
Time: 22:30 (25) | Contributor: Salesman
Time: 22:30 (25) | Date: 2011
EOT;
$regex = '~^(?:.*?\|\s*)(Date:\s*((?:\d{2} \S+ )?\d{4}))?(?:\s\|\s*)?(Contributor:\s*(\S+))?\s*$~m';
preg_match_all($regex, $data, $matches);
print_r($matches);
?>
Output:
Array
(
[0] => Array
(
[0] => Time: 22:30 (25) | Date: 19 March 2011
[1] => Time: 22:30 (25) | Date: 2011 | Contributor: Salesman
[2] => Time: 22:30 (25) | Contributor: Salesman
[3] => Time: 22:30 (25) | Date: 2011
)
[1] => Array
(
[0] => Date: 19 March 2011
[1] => Date: 2011
[2] =>
[3] => Date: 2011
)
[2] => Array
(
[0] => 19 March 2011
[1] => 2011
[2] =>
[3] => 2011
)
[3] => Array
(
[0] =>
[1] => Contributor: Salesman
[2] => Contributor: Salesman
[3] =>
)
[4] => Array
(
[0] =>
[1] => Salesman
[2] => Salesman
[3] =>
)
)

Insert element into order position of array?

All values of array $A are string the same length.
$A = Array
(
[0] => 03
[1] => 04
[2] => 05
[3] => 06
// [4] => 07 // "07" before "04" position
[4] => 04
[5] => 05
[6] => 06
// [8] => 07 // "07" before "08" position
[7] => 08
[8] => 03
[9] => 04
[10] => 05
[11] => 06
[12] => 07 // it is existing
[13] => 08
) ;
I want to Insert the "07" element if it is not existing before "04" or "08" position.start from position 1
So It will be after changed
$A = Array
(
[0] => 03
[1] => 04
[2] => 05
[3] => 06
[4] => 07 // just appended
[5] => 04
[6] => 05
[7] => 06
[8] => 07 // just append
[9] => 08
[10] => 03
[11] => 04
[12] => 05
[13] => 06
[14] => 07
[15] => 08
) ;
Anybody know how to do this ,help me please?
There would be "prettier" ways to do this but, as intended...
iterate the array
if the current value is equal to 7 minus 1 you will insert a new value there
create a function "insert_into_array" that:
a) Splits your array in two (look at array_chunk)
b) POPs your element to the end of the first array (array_pop)
c) merges your two arrays back (array_merge)
I've abstained from writing any code as this is probably homework and, writing code, even if you're not really deep thinking the problem will push you a long way to passing the exam...
not the most beautiful solution, but should do the job:
$b = array();
for($i=0;$i<count($A);$i++){
$b[] = $A[$i];
if(($i<count($A) - 1) && ($A[$i+1]<$A[$i] || ($A[$i+1] == '08')) && $A[$i] < '07')
$b[] = '07';
}
var_dump($b);
First, find the gaps in your array, that is the positions where there's 06 but not a following 07:
$positions = array();
foreach ($A as $k => $v) {
if (isset($last) && $last != $v - 1 && $last == '06') {
$positions[] = $k;
}
$last = $v;
}
Then, insert them:
$count = 0;
foreach ($positions as $pos) {
array_splice($A, $pos + ($count++), 0, '07');
}
That's it.
//make sure the array is numeric:
$A = array_values($A);
foreach(array('04','08') as $search){
$positions = array_keys($A,$search);
rsort($positions);
foreach($positions as $key){
if($key==0 || $A[$key-1] != '07'){
array_splice($A,$key,0,'O7');
}
}
}
In 2017, I've found 2 beautiful methods that is part of nette\utils package.
Arrays::insertBefore()
Arrays::insertAfter()
They do job perfectly!
Just run:
composer require nette/utils
and use Arrays class or inspire in their code.

Categories