Help on pregreplace
am 18.08.2009 16:00:20 von Merlin Morgenstern
Hi there,
I am highlighting keywords with the help of pregreplace. This works
great with one limitation. If the word that has to be replaced contains
a slash, preg throws an error. So far I could not find a fix. Can
someone help?
Here is the code:
$pattern = "/\b($words)\b/is";
$replace = '\\1';
return preg_replace($pattern,$replace,$str);
Thank you in advance,
Merlin
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Re: Help on pregreplace
am 18.08.2009 16:07:46 von Ashley Sheridan
On Tue, 2009-08-18 at 16:00 +0200, Merlin Morgenstern wrote:
> Hi there,
>
> I am highlighting keywords with the help of pregreplace. This works
> great with one limitation. If the word that has to be replaced contains
> a slash, preg throws an error. So far I could not find a fix. Can
> someone help?
>
> Here is the code:
>
>
> $pattern = "/\b($words)\b/is";
> $replace = '\\1';
> return preg_replace($pattern,$replace,$str);
>
> Thank you in advance,
>
> Merlin
>
Well, a slash has a special meaning inside PHP strings, more so for
double quoted strings. Are you correctly escaping the slash as a double
slash so that it's not interpreted by the string as an escaped
character, as you will need to as the preg_replace will be interpreting
it as an escape sequence to match?
Thanks,
Ash
http://www.ashleysheridan.co.uk
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Re: Help on pregreplace
am 18.08.2009 16:19:53 von Vladan Stefanovic
You should check out preg_quote() function which puts a backslash in front
of characters (escapes them) that have a special meaning in regular
expressions.
Regards,
Vladan Stefanovic
"Merlin Morgenstern" wrote in message
news:12.62.22194.004BA8A4@pb1.pair.com...
> Hi there,
>
> I am highlighting keywords with the help of pregreplace. This works great
> with one limitation. If the word that has to be replaced contains a slash,
> preg throws an error. So far I could not find a fix. Can someone help?
>
> Here is the code:
>
>
> $pattern = "/\b($words)\b/is";
> $replace = '
> style="background:#FF0000;color:#FCCCCC;">\\1';
> return preg_replace($pattern,$replace,$str);
>
> Thank you in advance,
>
> Merlin
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Re: Help on pregreplace
am 18.08.2009 16:51:07 von Al
Merlin Morgenstern wrote:
> Hi there,
>
> I am highlighting keywords with the help of pregreplace. This works
> great with one limitation. If the word that has to be replaced contains
> a slash, preg throws an error. So far I could not find a fix. Can
> someone help?
>
> Here is the code:
>
>
> $pattern = "/\b($words)\b/is";
> $replace = '\\1';
> return preg_replace($pattern,$replace,$str);
>
> Thank you in advance,
>
> Merlin
best insurance
http://us2.php.net/manual/en/function.preg-quote.php
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Re: Help on pregreplace
am 18.08.2009 16:56:55 von Merlin Morgenstern
Ashley Sheridan wrote:
> On Tue, 2009-08-18 at 16:00 +0200, Merlin Morgenstern wrote:
>> Hi there,
>>
>> I am highlighting keywords with the help of pregreplace. This works
>> great with one limitation. If the word that has to be replaced contains
>> a slash, preg throws an error. So far I could not find a fix. Can
>> someone help?
>>
>> Here is the code:
>>
>>
>> $pattern = "/\b($words)\b/is";
>> $replace = '\\1';
>> return preg_replace($pattern,$replace,$str);
>>
>> Thank you in advance,
>>
>> Merlin
>>
> Well, a slash has a special meaning inside PHP strings, more so for
> double quoted strings. Are you correctly escaping the slash as a double
> slash so that it's not interpreted by the string as an escaped
> character, as you will need to as the preg_replace will be interpreting
> it as an escape sequence to match?
>
> Thanks,
> Ash
> http://www.ashleysheridan.co.uk
>
>
>
HI, replacing the delimiter slash by ~ solved the problem. Thank you
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Re: Help on pregreplace
am 18.08.2009 17:12:03 von Tom Worster
On 8/18/09 10:56 AM, "Merlin Morgenstern" wrote:
>
>
> Ashley Sheridan wrote:
>> On Tue, 2009-08-18 at 16:00 +0200, Merlin Morgenstern wrote:
>>> Hi there,
>>>
>>> I am highlighting keywords with the help of pregreplace. This works
>>> great with one limitation. If the word that has to be replaced contains
>>> a slash, preg throws an error. So far I could not find a fix. Can
>>> someone help?
>>>
>>> Here is the code:
>>>
>>>
>>> $pattern = "/\b($words)\b/is";
>>> $replace = '\\1';
>>> return preg_replace($pattern,$replace,$str);
>>>
>>> Thank you in advance,
>>>
>>> Merlin
>>>
>> Well, a slash has a special meaning inside PHP strings, more so for
>> double quoted strings. Are you correctly escaping the slash as a double
>> slash so that it's not interpreted by the string as an escaped
>> character, as you will need to as the preg_replace will be interpreting
>> it as an escape sequence to match?
>>
>> Thanks,
>> Ash
>> http://www.ashleysheridan.co.uk
>>
>>
>>
> HI, replacing the delimiter slash by ~ solved the problem. Thank you
which means that words with ~ in them will fail. as Al pointed out,
preg_quote() is a more general solution. it escapes all tricky pcre
characters as well as the delimiter.
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Re: Help on pregreplace
am 19.08.2009 16:26:07 von TedD
At 10:51 AM -0400 8/18/09, Al wrote:
>Merlin Morgenstern wrote:
>>Hi there,
>>
>>I am highlighting keywords with the help of pregreplace. This works
>>great with one limitation. If the word that has to be replaced
>>contains a slash, preg throws an error. So far I could not find a
>>fix. Can someone help?
>>
>>Here is the code:
>>
>>
>> $pattern = "/\b($words)\b/is";
>> $replace = '\\1';
>> return preg_replace($pattern,$replace,$str);
>>
>>Thank you in advance,
>>
>>Merlin
>
>best insurance
>
>http://us2.php.net/manual/en/function.preg-quote.php
In addition, you might consider moving your style attributes to a
style sheet and then using a class name like so:
$replace = '\\1';
That way you can change highlighting as you want without altering your code.
Cheers,
tedd
--
-------
http://sperling.com http://ancientstones.com http://earthstones.com
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php