Jump to content

Recommended Posts

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.

Link to comment
https://forums.phpfreaks.com/topic/182027-replacing-whole-phrases/
Share on other sites

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...

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>';

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!
?>

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.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.