Reputation: 1
I am trying to fetch the date from a giant text string when a certain word is meet. So in the text field below where it says "polis", i want the date 29/7/2017
So the pattern field is the specific word I am after in this case "polis", subject is the array that contains the entire textfield.
22/10/2013 ånger, ska bet 6,428:-, bet 6428, 2109+2016+2303 inlagt 23/5/2014 bl 26/5, medd inga avgifter) 29/7/2014 spelmissbruk 29/7/2014 polis 29/12/2014 föreslår godkänn 27/4/2016 Föreslår godkänn
Here is what I have tried:
$subject = $row['anteckning'];
$pattern = $wordToFind;
preg_match("/$pattern/",$subject,$matchWord);
//print_r($matchWord);
if($matchWord[0] == $pattern) {
$test1 = strstr($subject, $matchWord[0]);
$testword = substr($test1, 0,8);
echo $testword;
}
My problem is that i keep getting the first date 22/10/2013.
Upvotes: 0
Views: 72
Reputation: 23968
You can use .*
as a wildcard on each side of the string in preg_match_all to capture all in one.
I use i
to make it case insensitive to make sure it captures polis, Polis, POLIS
and so on.
EDIT: I see now you only want the dates. Added a capture of the dates to the regex.
$matches[1] will hold the dates only and $matches[0] will hold the complete line.
preg_match_all('/(\d{1,2}\/\d{1,2}\/\d{4}).*polis.*/i', $str, $matches);
var_dump($matches);
output: (my example)
array(2) {
[0]=>
array(2) {
[0]=>
string(15) "29/7/2014 polis"
[1]=>
string(15) "28/4/2016 POLIS"
}
[1]=>
array(2) {
[0]=>
string(9) "29/7/2014"
[1]=>
string(9) "28/4/2016"
}
}
Upvotes: 0
Reputation: 8358
preg_match_all("/$pattern/",$subject,$matchWord);
You are using preg_match
so get's you the first only value that finds.
Of course you will have to make some modifications to your code but you will get all the values based on preg_match_all
function so you will handle them as you like.
Upvotes: 1