fork download
  1. <?php
  2. $special_words = array("dr.", "a.sh.", "a.k");
  3. array_walk($special_words, function(&$item, $key){ $item= preg_quote($item, '~');});
  4.  
  5. $regex = '~(?<!\w)(' . implode('|', $special_words) . '|\w+)(?!\w)~';
  6. $str = 'word word, dr. word: a.sh. word a.k word?!..';
  7. preg_match_all($regex, $str, $matches);
  8. var_dump($matches[0]);
  9. ?>
Success #stdin #stdout 0.01s 20568KB
stdin
Standard input is empty
stdout
array(8) {
  [0]=>
  string(4) "word"
  [1]=>
  string(4) "word"
  [2]=>
  string(3) "dr."
  [3]=>
  string(4) "word"
  [4]=>
  string(5) "a.sh."
  [5]=>
  string(4) "word"
  [6]=>
  string(3) "a.k"
  [7]=>
  string(4) "word"
}