gerkintrigg Posted November 18, 2009 Share Posted November 18, 2009 Hi everyone. You may remember a post earlier this month about replacing words. Well I worked that out but now I'm having an issue with replacing whole phrases. I think it's because I only want to replace whole words like "you" but not replace them when they are within other words like "yourself". This is the old code, which I am using with a preg function and works fine for single words: $pattern='~\b'.$word.'\b(?![^<]*?>'~ I know it's the \b that's causing the issue but my question is this: How do I create a query within the preg_replace() function that looks for whether the $word variable has a space in it and removes the \b if it does? currently my preg_replace() looks like this: $page = preg_replace($pattern, $new_word, $page); Thanks in advance. Quote Link to comment https://forums.phpfreaks.com/topic/182027-replacing-whole-phrases/ Share on other sites More sharing options...
Ken2k7 Posted November 18, 2009 Share Posted November 18, 2009 Hello gerkintrigg, Isn't that just a really redundant way of saying replacing the word "you" with another word? <?php $page = str_replace($word, $new_word, $page); Ken Quote Link to comment https://forums.phpfreaks.com/topic/182027-replacing-whole-phrases/#findComment-960280 Share on other sites More sharing options...
gerkintrigg Posted November 19, 2009 Author Share Posted November 19, 2009 no because I want to ignore HTML tags and keep whole words too so a simple str_replace won't necessarily do the job. Quote Link to comment https://forums.phpfreaks.com/topic/182027-replacing-whole-phrases/#findComment-960521 Share on other sites More sharing options...
newbtophp Posted November 19, 2009 Share Posted November 19, 2009 <?php //wordtoreplace $word = "test"; $pattern='~\b'.$word.'\b(?![^<]*?>'~ if(preg_match('/ +/', $word)) { $pattern='~'.$word.'(?![^<]*?>'~ } $new_word = "replacewith"; $page = preg_replace($pattern, $new_word, $page); ?> Quote Link to comment https://forums.phpfreaks.com/topic/182027-replacing-whole-phrases/#findComment-960537 Share on other sites More sharing options...
emopoops Posted November 19, 2009 Share Posted November 19, 2009 is the whole thing u want to replace you but not yourself. just add a space hon Quote Link to comment https://forums.phpfreaks.com/topic/182027-replacing-whole-phrases/#findComment-960596 Share on other sites More sharing options...
gerkintrigg Posted November 19, 2009 Author Share Posted November 19, 2009 emopoops: no, i want to be able to replace phrases like "I like you" to something that highlights it using a span and class but i don't want to pick up "I like your shoes". I also want to ensure that if it ends in a "." then it will still pick it up and I want to avoid alt tags and other code and I also want it to run through a database and replace phrases from the tables. I also want to be able to count each string that has been matched but that's for another post / topic... Quote Link to comment https://forums.phpfreaks.com/topic/182027-replacing-whole-phrases/#findComment-961027 Share on other sites More sharing options...
cags Posted November 19, 2009 Share Posted November 19, 2009 The whole point of \b is word boundary. Since you are placing the value you wish to replace between \b items it will only match a 'sentence' that begins and ends with a word boundary. So given your example of "I like you", "#\bI like you\b#" will not match I like your shoes. Since the fullstop is a word boundary "I like you." will match (but note it will not include the punctuation in the capture group since it's technically not part of your pattern). If you wish grammer to be included, I think you will have to add them to your pattern. $word = preg_quote("I like you", "#"); $pattern = "#\b$word\b[.,!?]?#"; $input = "I like you, I like your shoes, I like you. I like you cos your cool."; preg_match_all($pattern, $input, $out); echo '<pre>'; print_r($out); echo '</pre>'; Quote Link to comment https://forums.phpfreaks.com/topic/182027-replacing-whole-phrases/#findComment-961032 Share on other sites More sharing options...
thebadbad Posted November 19, 2009 Share Posted November 19, 2009 I don't see why your current code shouldn't work? <?php $word = 'I like you'; $pattern = '~\b' . preg_quote($word, '~') . '\b(?![^<]*?>)~i'; $str = 'I like you. I like your shoes.'; echo preg_match($pattern, $str) . ' match found!'; //1 match found! ?> Quote Link to comment https://forums.phpfreaks.com/topic/182027-replacing-whole-phrases/#findComment-961037 Share on other sites More sharing options...
cags Posted November 19, 2009 Share Posted November 19, 2009 Looking back at it, I'm guessing the OP would like "I <b>like</b> you" to also match, which will be alot more complicated. Quote Link to comment https://forums.phpfreaks.com/topic/182027-replacing-whole-phrases/#findComment-961039 Share on other sites More sharing options...
gerkintrigg Posted November 19, 2009 Author Share Posted November 19, 2009 cags: Yes, that'd be the next stage but at the moment I think we can safely assume that this is going to be one stage too far for now. I'll do a bit more testing and see whether the current code is valid by changing some database entries... I have tested it before though... perhaps the lateness of the hour impinged my cognitive function. i shall try again now. Quote Link to comment https://forums.phpfreaks.com/topic/182027-replacing-whole-phrases/#findComment-961194 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.