good chemistry is complicated, and a little bit messy -LW |
|
PerlMonks |
Perl RegEx in PHPby YAFZ (Pilgrim) |
on Aug 11, 2003 at 14:00 UTC ( [id://282877]=perlquestion: print w/replies, xml ) | Need Help?? |
YAFZ has asked for the wisdom of the Perl Monks concerning the following question:
Dear Monks,
My brother is programming a Turkish website about jazz and he does that in PHP. A few weeks ago we implemented a search mechanism for it. Then we decided to color the results, for example you search for Herbie Hancock, it gives you the results and when you view the related web page the keywords Herbie and Hancock are in a different color to attract attention and easy browsing. To achieve a simple result and help my brother I've decided to use Perl Compatible RegEx in PHP and it worked ok. All it did was to search for keywords one by one, case-insensitive and then replace the matching pattern with font and color tags around it. That was ok until I realized that it also matched the very same keywords in URLs and IMG, ALT, etc. tags. For example if you search for Herbie Hancock and then visit this page you'll see that there is a problem. Herbie Hancock is in different color but so is the image URL! (This is the reason of your not seeing a nice image of Hancock, try it by deleting the keywords parameter from URL ;-) E.g. The same thing applies to strings like which is converted into something like To solve the problem I've done the coloring first and then tried to search for the problematic parts and remove the surrounding FONT tags. It worked for the URLs and now I have to do it for IMG, ALT, etc. This makes me feel a little bit uneasy as you can imagine. Is there any way to tell the PCRE engine(Perl Compatible RegEx library in PHP) this: Just do the coloring on the keywords that are surrounded by HTML tags like a href, IMG, ALT, etc. I'm not very experienced with look forward and backward operations in using RegEx and I'm also not sure if they are supported by PCRE. Any ideas that can lead to a better solution will be apprreciated. I'm sorry for this PHP question but since it is Perl compatible RegEx stuff and since the grandmasters of the RegEx live here, I thought I could get help :)
Back to
Seekers of Perl Wisdom
|
|