Absolute String

Any questions involving matching text strings to patterns - the pattern is called a "regular expression."

Moderator: General Moderators

Post Reply
User avatar
Technocrat
Forum Contributor
Posts: 127
Joined: Thu Oct 20, 2005 7:01 pm

Absolute String

Post 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?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

that technically is an absolute string...

use the word boundry metacharacter: \b
User avatar
Technocrat
Forum Contributor
Posts: 127
Joined: Thu Oct 20, 2005 7:01 pm

Post 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?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

what are each of the variables?
User avatar
Technocrat
Forum Contributor
Posts: 127
Joined: Thu Oct 20, 2005 7:01 pm

Post 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 "***"
User avatar
Technocrat
Forum Contributor
Posts: 127
Joined: Thu Oct 20, 2005 7:01 pm

Post 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
RobertPaul
Forum Contributor
Posts: 122
Joined: Sun Sep 18, 2005 8:54 pm
Location: OCNY

Post by RobertPaul »

Yea, I deleted it because I realized it was a stupid suggestion. :oops: Sorry!
User avatar
Technocrat
Forum Contributor
Posts: 127
Joined: Thu Oct 20, 2005 7:01 pm

Post 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 ***"
User avatar
Technocrat
Forum Contributor
Posts: 127
Joined: Thu Oct 20, 2005 7:01 pm

Post 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
RobertPaul
Forum Contributor
Posts: 122
Joined: Sun Sep 18, 2005 8:54 pm
Location: OCNY

Post 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
User avatar
Technocrat
Forum Contributor
Posts: 127
Joined: Thu Oct 20, 2005 7:01 pm

Post by Technocrat »

:lol: Its ok so am I, I am on hour 12 of my shift :(
Post Reply