regex to ignore case and punctuation

Hi, Please see the attached code that uses regex to find a phrase and then echos the result along with a certain number of words before and after.

The attached code produces this result:
... secret of a Cape Cod vacation close to their ...
... re considering a Cape Cod vacation here are some ...

see http://70.87.107.194/~g3crmco/aphrasetest5.php for that result


The issue that I am having is that 2 things prevent the code from generating the results that I am seeking.

1) If the $subject text "Cape Cod Vacation" does not match the complete case of $pattern, it will ignore it. I would like the script to ignore case.
2) If there is any punctuation in $subject it throws off the result. I would like the scrip to ignore punctuation.

Can someone please help me fix the script to achieve the described result which would be:

... secret of a Cape Cod vacation close to their ...
... trek for a Cape Cod vacation, many more people ...
... you’re considering a Cape Cod vacation, here are some ...

regardless of punctuation or case.

Thanks!




<?php

$subject= "For decades, families in the Boston area held the secret of a Cape Cod vacation close to their hearts like a family treasure. Every Friday, scores of station wagons loaded with escapees from the city would file over the Bourne Bridge to the East Coast's most enchanting vacation spot. Today, the word is out. While local Bostonians still make the trek for a Cape Cod vacation, many more people are crowding onto this coastal gem to take advantage of its amazing Atlantic views, superb seafood, and unique New England attitude. If you're considering a Cape Cod vacation, here are some of the venues and attractions you should put on your list. ";
$pattern= "/(\w+\s){3}Cape\sCod\svacation/";

preg_match_all($pattern, $subject, $matches);

//var_dump($matches);
$matches = $matches[0];

foreach($matches as $val)
	echo "... " . $val . " ...<br />";

?>

Open in new window

chrisj1963Asked:
Who is Participating?
 
Ray PaseurCommented:
To ignore case, add the letter "i" after the closing regex delimiter, in this case you would use something like this:

$pattern= "/(\w+\s){3}Cape\sCod\svacation/i";

I'll settle for half a loaf and let someone else help with the punctuation.  All the best, ~Ray
0
 
chrisj1963Author Commented:
Perfect RAy, thank you.  While waiting I solved the other 1/2 so you get it all...thanks very much...  (see code for full solution)

<?php

$subject= "For decades, families in the Boston area held the secret of a Cape Cod vacation close to their hearts like a family treasure. Every Friday, scores of station wagons loaded with escapees from the city would file over the Bourne Bridge to the East Coast's most enchanting vacation spot. Today, the word is out. While local Bostonians still make the trek for a Cape Cod vacation, many more people are crowding onto this coastal gem to take advantage of its amazing Atlantic views, superb seafood, and unique New England attitude. If you're considering a Cape Cod vacation, here are some of the venues and attractions you should put on your list. ";
//$pattern= "/(\w+\s){3}Cape\sCod\svacation/";

$subjectnew = preg_replace('/[^a-zA-Z0-9 ]/','',$subject);
$pattern=  "/(\w+\s){3}Cape Cod Vacation(\s\w+){3}/i";

preg_match_all($pattern, $subjectnew, $matches);

//var_dump($matches);
$matches = $matches[0];

foreach($matches as $val)
	echo "... " . $val . " ...<br />";
	
?>

Open in new window

0
 
chrisj1963Author Commented:
Thank you!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.