help with a PHP regex to extract text around a string
I am trying to use a regular expression and it's just breaking my brain. I'll think I have it and the the script just breaks. I need to try to give people a way to report their location via SMS. I ask them to text me a string that looks like this:
I give up, stopping for the night. Find me at Main Street and Pine, Atlanta, GA.
And I want to break it at Find me at
. I need to capture everything to the right of that (essentially their street 开发者_C百科corner) and I need to be able to detect if they didn't use proper capitalization for Find
.
Any suggestions?
preg_match("/[Ff]ind me at (.+)/", "I give up, find me at street 1 and street 2")
will capture everything after the "find me at"
The following regex will work for you:
Find me at (.*)$
If you want it to ignore the case, you can specify it using the IgnoreCase option (?i:)
Give this a shot: /find me at (.*)$/i
Well, a non-regexp solution comes to mind.
$string = 'I give up, stopping for the night. Find me at Main Street and Pine, Atlanta, GA.';
$findMe = 'Find me at';
$address = substr(strstr($string, $findMe), strlen($findMe));
if ($address == '') {
// no match
}
Then there is a regexp-based solution.
preg_match('/Find me at (.*)$/', $string, $match);
if (!isset($match[1])) {
// no match found
} else {
$address = $match[1];
}
I would just preg_split(). If you know what you want should always be at the end.
$str = "I give up, stopping for the night. Find me at Main Street and Pine, Atlanta, GA."
$strSplit = preg_split("/Find me at /i", $str);
$whatYouWant = $strSplit[1];
精彩评论