Link to home
Start Free TrialLog in
Avatar of chrisj1963
chrisj1963

asked on

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

ASKER CERTIFIED SOLUTION
Avatar of Ray Paseur
Ray Paseur
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of chrisj1963
chrisj1963

ASKER

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

Thank you!