?
Solved

regex to ignore case and punctuation

Posted on 2011-05-09
3
Medium Priority
?
561 Views
Last Modified: 2012-05-11
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

0
Comment
Question by:chrisj1963
  • 2
3 Comments
 
LVL 111

Accepted Solution

by:
Ray Paseur earned 2000 total points
ID: 35725227
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
 

Author Comment

by:chrisj1963
ID: 35725259
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
 

Author Closing Comment

by:chrisj1963
ID: 35725261
Thank you!
0

Featured Post

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
This article discusses how to implement server side field validation and display customized error messages to the client.
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
Suggested Courses

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question