help with regular expressions: Warning: Unknown modifier

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
fgomez
Forum Commoner
Posts: 61
Joined: Mon Sep 26, 2005 11:23 pm
Location: Washington, DC

help with regular expressions: Warning: Unknown modifier

Post by fgomez »

Hello,

The website I'm working on has a search function which displays the results with matches for the search term in bold. This effect is achieved with a simple snippet:

Code: Select all

$description = str_replace($q, "<strong>$q</strong>", $description);
The problem is that sometimes the search term is part of a URL in an <a> tag, which makes for the following undesirable output:

Code: Select all

<a href="http://www.<strong>searchterm</strong>.com/">some hyperlinked text</a>
After adding a little code to rectify this (to follow), I get the following PHP warning:
Warning: Unknown modifier ']' in /food/bar/search3.php on line 41
Here are lines 40 and 41 of the new code:

Code: Select all

$needle = "<a[^>]*>.*(".preg_quote($q)."|".preg_quote($q_uc).").*</a>" ;
if(!preg_match($needle, $description)) { /*some stuff*/ }
The idea was to highlight the search term only for records where the search term does not appear between <a> and </a>. This was supposed to be a quick fix; ideally, I'd like to be able to turn off the highlighting just for matches within the tags.

For example, I'd like the following to work:

Code: Select all

I like <strong>coffee</strong>.  Visit <a href="http://www.coffee.com/">www.<strong>coffee</strong>.com</a>.
The way I've written it (which doesn't even work), there would be no bolding at all.

Can anyone help with this? I especially need help with the unknown modifier warning; I might be able to figure out the rest on my own, though some help there would be appreciated as well. :wink:

Thanks,
User avatar
Burrito
Spockulator
Posts: 4715
Joined: Wed Feb 04, 2004 8:15 pm
Location: Eden, Utah

Post by Burrito »

you need to add a delimiter in your pattern:

ex: untested

Code: Select all

$needle = "/<a[^>]*>.*(".preg_quote($q)."|".preg_quote($q_uc).").*</a>/" ;
fgomez
Forum Commoner
Posts: 61
Joined: Mon Sep 26, 2005 11:23 pm
Location: Washington, DC

Post by fgomez »

I've never heard of a delimiters in this context before. Haven't tried the code yet, will let you know when I do. In the meantime, can you point me to some documentation on delimiters? Sounds like something I need to know!
User avatar
Burrito
Spockulator
Posts: 4715
Joined: Wed Feb 04, 2004 8:15 pm
Location: Eden, Utah

Post by Burrito »

here is the best regex tutorial in the entire world...it explains all about them:

viewtopic.php?t=33147
fgomez
Forum Commoner
Posts: 61
Joined: Mon Sep 26, 2005 11:23 pm
Location: Washington, DC

Post by fgomez »

Checked out the tutorial, added the delimiters, still no luck. The warning has changed though. Now it says:
Warning: Unknown modifier 'a' in /food/bar/search3.php on line 41
whereas before it said
Warning: Unknown modifier ']' in /food/bar/search3.php on line 41
Arg... I've spent waaay too much time on this. Help?!?!?
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Post by Weirdan »

you need to escape the delimiter in the "</a>" part with backslash:

Code: Select all

$needle = "/<a[^>]*>.*(" . preg_quote($q, "/") . "|" . preg_quote($q_uc, "/") . ").*<\/a>/" ;
fgomez
Forum Commoner
Posts: 61
Joined: Mon Sep 26, 2005 11:23 pm
Location: Washington, DC

Post by fgomez »

Thank you. The warning is gone.

Usually I use ereg instead of preg_match. Does ereg not require the delimiters? I've never used them before and never experienced problems.

Hmm... the script is still not behaving the way I think it should. I probably need to look at the regex again. I guess I'll rebuild it bit by bit. It's not nonsensical though, is it?

Thanks for all your help.
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Post by Weirdan »

Usually I use ereg instead of preg_match. Does ereg not require the delimiters? I've never used them before and never experienced problems.
Posix regexps (ereg* family) do not require delimiters. Perl regexps (preg_*) do.
Hmm... the script is still not behaving the way I think it should.
what is $q_uc ? $q in upper case?
fgomez
Forum Commoner
Posts: 61
Joined: Mon Sep 26, 2005 11:23 pm
Location: Washington, DC

Post by fgomez »

Code: Select all

$q_uc = ucwords($q);
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Post by Weirdan »

I guess for search term highlighting you'd better use case insensitive regexp:

Code: Select all

$needle = "/<a[^>]*>.*?" . preg_quote($q, "/") . ".*?<\/a>/i" ;
Also I would make .* parts non-greedy (add ? to them).
fgomez
Forum Commoner
Posts: 61
Joined: Mon Sep 26, 2005 11:23 pm
Location: Washington, DC

Post by fgomez »

Nope, same misbehavior. In any case, to prevent the app from breaking, I've done this in the meantime:

Code: Select all

$needle = "/<a/" ;
This turns off highlighting for any record with an <a> tag in the description. I'm willing to live with this until I have a little more time to look into it. I'll post a more elegant solution when I get around to working on it.

Thanks, everyone, for your help. The delimiters thing was not covered in any other regex tutorials I read before (which were not geared towards PHP), and I wouldn't have figured that out on my own this quickly.
Post Reply