0

I'm trying to replace the first (is|are) in a string to (is not|are not), but ONLY the first part. I want it to skip any other (is|are) it finds in the string. How would I be able to do that?

$text = "it is what they are";
$replacedText = preg_replace("~\b(is|are)\b~", "$1 not", $text);

Actual Result:

it is not what they are not

Need Result:

it is not what they are

frosty
  • 2,559
  • 8
  • 37
  • 73
  • 1
    You could use the `$limit` from preg_replace. See https://stackoverflow.com/questions/6729710/replace-only-first-match-using-preg-replace – The fourth bird Mar 24 '19 at 08:32
  • @Thefourthbird I want to say that the parameter is the fourth one, and your name is the fourth bird. Coincidence? I think not. – frosty Mar 24 '19 at 08:35

0 Answers0