Reputation: 121
I have a string, I need to search this string and be able to assign the address details to a variable:
how it looks in the string:
infoone:"infoone"infotwo:"infotwo"address:"123 fake street pretend land"infothree:"infothree"infofour:"infofour" address:"345 fake street pretend land"infofive: "infofive"infosix: "infosix"
How would I use regular expressions to search through this string to lift only the data in the inverted commas after the word address,?
Note: I cannot target the phrase "123 fake street pretend land" as this is just an example used of what might be in the inverted commas.
Upvotes: 0
Views: 119
Reputation: 1048
Use this regex
$str='infoone:"infoone"infotwo:"infotwo"address:"123 fake street pretend land"infothree:"infothree"infofour:"infofour" address:"345 fake street pretend land"infofive: "infofive"infosix: "infosix"';
preg_match_all("/address:\"(.*)\"/siU",$str,$out);
print_r($out[1]);
Upvotes: 1
Reputation: 14040
This is a fine regex
^address:"([^"]*)
This is it in php with the option so that ^ matches at the beginning of the line and we lift out group 1
preg_match_all('/^address:"([^"]*)/m', $subject, $result, PREG_PATTERN_ORDER);
$result = $result[1];
Update 1
preg_match_all('/^address:"([^"]*)/m', $subject, $result, PREG_SET_ORDER);
for ($matchi = 0; $matchi < count($result); $matchi++) {
for ($backrefi = 0; $backrefi < count($result[$matchi]); $backrefi++) {
# Matched text = $result[$matchi][$backrefi];
}
}
Update 2
With the new sample input just leave at the ^ in the beginning so it becomes
address:"([^"]*)
Upvotes: 2
Reputation: 1761
Well, the regexp itself would be ^address:"(.*)"$
.
Obviously, you'll need to add relevant preg_match()
call.
Upvotes: 1