Page 1 of 1

Absolute String

Posted: Thu Oct 20, 2005 7:29 pm
by Technocrat
I have been trying to figure out how to replace an absolute string.

Take for example:
"The man in there was on the theme for the"

Lets say I want to only replace the word "the". But when I try to use preg_replace I seem to catch there and theme because it has the in it. So how do I get and absolute string?

Posted: Thu Oct 20, 2005 7:40 pm
by feyd
that technically is an absolute string...

use the word boundry metacharacter: \b

Posted: Thu Oct 20, 2005 7:51 pm
by Technocrat
Maybe I am doing this wrong then.

I have:
$message = preg_replace('/\b'.$word.'\b/', $replace, $message);
But that seems to not work. So what's the right way?

Posted: Thu Oct 20, 2005 8:18 pm
by feyd
what are each of the variables?

Posted: Thu Oct 20, 2005 8:23 pm
by Technocrat
$message = string of data to parse, in this case using the example "The man in there was on the theme for the"
$word = is a single string which is the word to look for, using the example "the"
$replace = A string of * = to the length of the word, using the example "***"

Posted: Thu Oct 20, 2005 9:01 pm
by Technocrat
Same problem as before:
"The man in ***re was on *** ***me for ***"
It misses the first the, and replaces words with the in them. Thats why I was trying preg_replace.

[EDIT] - :lol: He delete his post

Posted: Thu Oct 20, 2005 9:04 pm
by RobertPaul
Yea, I deleted it because I realized it was a stupid suggestion. :oops: Sorry!

Posted: Thu Oct 20, 2005 9:05 pm
by Technocrat
I played with it some more and I found a small bug I had which got me closer. But I am missing the first the.
"The man in there was on *** theme for ***"

Posted: Thu Oct 20, 2005 9:09 pm
by Technocrat
I got it :)

Here is the solution for anyone else:
$message = preg_replace('/\b'.$word.'\b/i', $replace, $message);

Thanks for the help

Posted: Thu Oct 20, 2005 9:11 pm
by RobertPaul
Probably a case sensitivity thing. Add 'i' after the patter delimiter. i.e. $pattern = "/\bthe\b/i"

[EDIT] Oy I'm slow tonite. 8O

Posted: Thu Oct 20, 2005 9:15 pm
by Technocrat
:lol: Its ok so am I, I am on hour 12 of my shift :(