Fetch Date and a String from another string - php

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

Related

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
)

Array merge while keeping the same order?

I've got an array that is an array of arrays, ordered by date, as so:
Array
(
[1379167200] => Array
(
[110] => Introduction to Banking | Saturday, September 14, 2013 - 10:00am
)
[1380376800] => Array
(
[71] => Saving, Investing, Debt | Saturday, September 28, 2013 - 10:00am
)
[1381588200] => Array
(
[72] => Setting Personal Goals | Saturday, October 12, 2013 - 10:30am
)
[1382796000] => Array
(
[74] => Type of Account: What's Right for You? | Saturday, October 26, 2013 - 10:00am
)
[1384009200] => Array
(
[81] => Creating an Account: Learning to Budget | Saturday, November 09, 2013 - 10:00am
)
)
I want to keep the ordering (i.e., ordered by date), but preferably only include the most inner array items in a single array. Like so:
Array
(
[110] => Introduction to Banking | Saturday, September 14, 2013 - 10:00am
[71] => Saving, Investing, Debt | Saturday, September 28, 2013 - 10:00am
[72] => Setting Personal Goals | Saturday, October 12, 2013 - 10:30am
[74] => Type of Account: What's Right for You? | Saturday, October 26, 2013 - 10:00am
[81] => Creating an Account: Learning to Budget | Saturday, November 09, 2013 - 10:00am
)
How would this be possible? Would I need to convert the integers to strings or something?
I absolutely need to maintain the ordering by date, but I also need to maintain the relationship between the integer key and the value, both are used.
Iterate through array, create new array:
$newArray = Array();
foreach($array as $row) { // $row is subarray
$value = current($row); // first value in subarray "Itroduction to ..."
$key = key($row); // first key in subarray 101, 71, 72
$newArray[$key] = $value;
}
Demo;
http://codepad.viper-7.com/dd3PLV
Docs:
http://php.net/manual/en/function.current.php
http://php.net/manual/en/function.key.php

Find date after a particular word

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

display Array within ul li tags

I have an array that looks like this and I want to display it in a more readable format. I would like the bird name (Gray Hawk etc) and then each listing (which the number of result will vary. This is the code I currently have:
$xml = simplexml_load_file($noteable);
$result = array();
foreach ($xml->result->sighting as $sighting) {
$location = (string) $sighting->{'loc-name'};
$bird = (string) $sighting->{'com-name'};
$howMany = (string) $sighting->{'how-many'};
$obsdt = (string) $sighting->{'obs-dt'};
$thenotedate = $obsdt;
$thenotedate = split('T',$thenotedate);
$thenotedate = $thenotedate[0];
$thenotedate = strftime('%a %b %e at %I:%M %p',strtotime($thenotedate));
ksort($result);
if (!isset($result[$bird])) $result[$bird] = array();
$result[$bird][] = $howMany . ' seen at ' . $location . ' on ' . $thenotedate;
}
print"<pre>";
print_r($result);
print"</pre>";
}
And this is the array
[Gray Hawk] => Array
(
[0] => 1 seen at Florida Canyon--lower on Sun Jun 2 at 04:50 PM
[1] => 1 seen at Madera Canyon--Whitehouse Picnic area on Sat Jun 1 at 07:30 AM
[2] => 1 seen at Florida Canyon--lower on Thu May 30 at 07:56 AM
[3] => 1 seen at Florida Canyon--lower on Wed May 29 at 07:40 AM
[4] => 1 seen at Florida Canyon--lower on Wed May 29 at 07:37 AM
[5] => 1 seen at Madera Canyon--Madera Picnic Area on Tue May 28 at 04:45 PM
[6] => 1 seen at Madera Canyon--Proctor Rd. on Mon May 27 at 09:40 AM
)
[MacGillivray's Warbler] => Array
(
[0] => 1 seen at Madera Canyon--Proctor Rd. on Sat May 25 at 05:45 PM
[1] => 1 seen at Madera Canyon--Proctor Rd. on Sat May 25 at 05:45 PM
[2] => 1 seen at Madera Canyon--Proctor Rd. on Sat May 25 at 05:30 PM
)
try something like this -
foreach ($result as $key=>$value){
//echo Bird Name
echo "<h3>$key</h3>";
//start an unordered list
echo "<ul>";
//echo each sighting
foreach($value as $row){
echo "<li>$row</li>";
}
//close the unordered list
echo "</ul>";
}
when printing out the result don't use print_r($result); use a loop which returns each element in the array! If you need infos on how to display the array as you want to tell us exactly how do you want it to be outputted

Populate array as per result of parent array

I have a parent array that contains
years as
1994,
1995,
1996,
1997,
1998,
1999,
2000,
2005
and on the other hand i am getting result from my db that returns year and count , Now, what i am trying to do is that an array being populated as per parent array (years) for the results being generated for something like following:
Results being Generated by sql
+----------------+
YEAR | Count
1994 | 16
1995 | 16
1996 | 16
+----------------+
The array should store following values as per check if sql query results contains result for year of parent array or not
1994 | 16
1995 | 16
1996 | 16
1997 | 0
1998 | 0
1999 | 0
2000 | 0
2005 | 0
Thanks,
Note I want the reuslt to be generated in an array as mentioned above because i have to pass these values to highcharts (For generating graph)
Depends how $years_db is built, Here an example:
$years = array(1994, 1995, 1996, 1997, 1998, 1999, 2000, 2005);
$years_db = array(1994 => 16, 1995 => 16, 1996 => 16);
$array_temp = array();
foreach ($years as $year)
{
/* if (in_array($year, array_keys($years_db)))
* $array_temp[$year] = $years_db[$year];
* else
* $array_temp[$year] = 0;
*/
// #gumbo suggestion is more efficient. THX!
$array_temp[$year] = isset($years_db[$year]) ? $years_db[$year] : 0;
}
output of $array_temp:
array (
1994 => 16,
1995 => 16,
1996 => 16,
1997 => 0,
1998 => 0,
1999 => 0,
2000 => 0,
2005 => 0,
)

Categories