Welcome to WebmasterWorld Guest from 23.22.140.143

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)

Junior Member

10+ Year Member

joined:Nov 2, 2003
posts:95
votes: 0


Hi,

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?

Cheers

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

Administrator

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

joined:July 31, 2003
posts:12533
votes: 0


Escape it.
\&
Also, have a look at preg_quote(). [php.net]
3:42 pm on Dec 22, 2004 (gmt 0)

Junior Member

10+ Year Member

joined:Nov 2, 2003
posts:95
votes: 0


Haha thats awesome mate! Muchos Gracias!