fork(1) download
  1. <?php
  2.  
  3. $dot_prod = "at the coast will reach the Douglas County coast";
  4. //=> at the <b>coast</b> will reach <b>the</b> Douglas County coast
  5.  
  6. //Specifically, I want to bold the word "coast" and "the" but only the word coast if not preceded by the
  7. //word "county" and only the word "the" if not preceded by the word "at". So, essentially I want an array
  8. //of words or phrases (case-insensitive that keeps the case the word/phrase was originally in)
  9. //to be bolded and then an array of words or phrases that I want to ensure are not bolded.
  10. //For instance, the array of words/phrases that I want bolded are:
  11.  
  12. $bold = array("coast", "the", "pass");
  13. //and the array of words I want to ensure are unbolded are:
  14.  
  15. $unbold = array("county coast", "at the", "grants pass");
  16. $rx = "~\b(?:" . implode("|", $unbold) . ")\b(*SKIP)(*F)|\b(?:" . implode("|", $bold) . ")\b~i";
  17. echo "$rx\n";
  18. echo preg_replace($rx, "<b>$0</b>", $dot_prod);
Success #stdin #stdout 0.01s 82624KB
stdin
Standard input is empty
stdout
~\b(?:county coast|at the|grants pass)\b(*SKIP)(*F)|\b(?:coast|the|pass)\b~i
at the <b>coast</b> will reach <b>the</b> Douglas County coast