Here is a simple but useful function to remove substrings within a string in PHP.
The second parameter can be a string or an array of strings, in this case all strings in the array will be removed from the string passed as the first argument.
/**
* Remove substring from string
*
* @param string $subject
* @param mixed $remove[optional] If omitted the function will remove whitespaces
* @return mixed
*/
function rmv($subject, $remove = " ") {
$return = $subject;
if (!is_array($remove)) {
$remove = array($remove);
}
for ($i = 0; $i < count($remove); $i++) {
$return = str_replace($remove[$i], "", $return);
}
return $return;
}
Examples:
rmv("Lorem ipsum dolor"); // return 'Loremipsumdolor';
rmv('mxaxuxix', 'x'); // return 'maui';
rmv("pizza", array("a", "i")); // return 'pzz';