Welcome to WebmasterWorld Guest from

Forum Moderators: coopster & jatar k

Message Too Old, No Replies

Regex help treating & as regular character



3:06 pm on Dec 22, 2004 (gmt 0)

10+ Year Member


I'm trying to highlight words in a string via regex. I not only want to highlight the singular word e.g. "word" but also plurals like "words" and "word's".

This is easy to do, but some strings use entities to represent single quotes, such as ’ and ’. This is were my problem is, because it appears & is being treated as an operator, instead of plain character.

I came up with a simple version that doesn't work:

$find = '/([^a-zA-Z0-9])(' . $keyword . 's¦' . $keyword . '\'s¦' . $keyword . '’s¦' . $keyword . '’s' . $keyword . ')([^a-zA-Z0-9])/i';

$replace = "\$1<b>\$2</b>\$3";

Any one know how i can get the regex parser to not interpret & as an operator?



3:40 pm on Dec 22, 2004 (gmt 0)

WebmasterWorld Administrator coopster is a WebmasterWorld Top Contributor of All Time 10+ Year Member

Escape it.
Also, have a look at preg_quote(). [php.net]


3:42 pm on Dec 22, 2004 (gmt 0)

10+ Year Member

Haha thats awesome mate! Muchos Gracias!

Featured Threads

Hot Threads This Week

Hot Threads This Month