JOOMLA FICTION LAB
(3 votes)

How to strip specific words from a string with php

Wednesday, 22 February 2012 11:46
With this simple php script you can remove any specified words from a string. This is very useful when you need to filter input to strip forbidden words.

Example


//strip specific words
$string = 'Nice useful handy bad ugly great';
$wordlist = array("bad", "ugly");
foreach ($wordlist as &$word) {
  $word = '/\b' . preg_quote($word, '/') . '\b/';
}					
$string = preg_replace($wordlist, '', $string);




Add comment


Security code
Refresh