0

I am trying to search and replace into a string, I have 2 problems 1. i need to check if term is inside a tag "term" - and do nothing in this case. 2. I need to make sure I am replacing a complte word and not cutting this word:

function str_replace_first($from, $to, $content)
{
    $from = '/'.preg_quote($from, '/').'/';

    return preg_replace($from, $to, $content, 1);
}

example:

echo str_replace_first('good','bad','goodmorning'); 

the result: badmorning // I need the script not to replace if we are cutting a word, and do the replace if we are changing a whole word thanks

Simba
  • 4,952
  • 3
  • 19
  • 29
  • `'/^'.preg_quote($from, '/').'$/'` – AbraCadaver May 10 '18 at 12:17
  • 1
    @Nimer Can we get some sample data and expected results? Please edit/improve your question. – mickmackusa May 10 '18 at 12:20
  • @AbraCadaver what is that? – mickmackusa May 10 '18 at 12:21
  • i think you will have to do it character-by-character and not use a regex for that as it does not have these requirements – Nikos M. May 10 '18 at 12:23
  • 1
    [quietly wishing I could downvote @Nikos untrue comment] – mickmackusa May 10 '18 at 12:23
  • whenever you see an open tag `<` mark it until closed, then replace the text and wait untill you see a close tag `<\` – Nikos M. May 10 '18 at 12:24
  • @mickmackusa a regex cannot match tags and what is inside in a consistent manner. This is a requirement of the OP. Of course if the text is only as simple as the example posted then yes it is possible – Nikos M. May 10 '18 at 12:26
  • @NikosM. Please avoid _trying_ to post solutions as comments. Comments are to seek improvements or ask for clarifications. – mickmackusa May 10 '18 at 12:26
  • 1
    @NikosM. Can regex be used: Yes. Should it be used in this case? We don't know because we don't have any input data. Rather than rush to a judgment, let's wait for clarification. – mickmackusa May 10 '18 at 12:27
  • Quitely agreeing with @mickmackusa last comment – RiggsFolly May 10 '18 at 12:29
  • @Nimer Are we dealing with well-formed html here? Or is this something unpredictable like user-supplied text? Can you show us a few different examples of what the input can be and what you expect to output? – mickmackusa May 10 '18 at 12:31
  • How close does this get you? [Regex to match words or phrases in string but NOT match if part of a URL or inside tags. php](https://stackoverflow.com/q/6009415/2943403) ...seems pretty close to me. – mickmackusa May 10 '18 at 12:32

1 Answers1

0

I need the script not to replace if we are cutting a word, and do the replace if we are changing a whole word

Luckily, the regular expression syntax has exactly what you need in the form of the \b word boundary marker. Simply add this to either end of the word you want to match, like so:

function str_replace_first($from, $to, $content)
{
    $from = '/\b'.preg_quote($from, '/').'\b/';

    return preg_replace($from, $to, $content, 1);
}

Now, the "good" in goodmorning will not be replaced, but good morning will be.

Simba
  • 4,952
  • 3
  • 19
  • 29