Replace word phrases, honoring punctuation in php

starcatcher3355

New Member
I have a custom php script that looks through text to find key phrases and replaces them using preg_replace, but I'm having trouble getting a clean regex for the job.For example this sentence:\[quote\] The fox jumped over the gated fence because it did not have hands to open the gate.\[/quote\]I would want to bold all instances of \[code\]gate\[/code\], yet respect oddities like \[code\]gated\[/code\]. And also preserve original capitalization too if possible.So the end sentence would read like this:\[quote\] The fox jumped over the gated fence because it did not have hands to open the gate.\[/quote\]Here is the regular expression I've created myself, and you will see that it would not account for gate. because of the period following the word. And also the regex does not respect any original capitalization. \[code\]$description = preg_replace("/ $keyphrase /", " <b>$keyphrase</b> ", $description, $limit);\[/code\]
 
Back
Top