mcleodm3
mcleodm3

Reputation: 167

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 corner) and I need to be able to detect if they didn't use proper capitalization for Find.

Any suggestions?

Upvotes: 4

Views: 870

Answers (5)

John
John

Reputation: 1540

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]; 

Upvotes: 0

erisco
erisco

Reputation: 14329

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];
}

Upvotes: 1

Crashspeeder
Crashspeeder

Reputation: 4311

Give this a shot: /find me at (.*)$/i

Upvotes: 1

dlev
dlev

Reputation: 48596

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"

Upvotes: 3

Oscar Mederos
Oscar Mederos

Reputation: 29863

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

Upvotes: 1

Related Questions