How can I strip unwanted characters / words from a PHP string? (Or, How can I remove or delete special characters from a PHP string?).
Remove characters from a string in php
function clean($string) { $string = str_replace(' ', '-', $string); // Replaces all spaces with hyphens. $string = preg_replace('/[^A-Za-z0-9\-]/', '', $string); // Removes special chars. return preg_replace('/-+/', '-', $string); // Replaces multiple hyphens with single one. } echo clean('a|"bc!@£de^&$f g');
Remove specific characters from string in php
str_replace php function is used to remove specific characters from a string in php.
$email = '[email protected]'; $email = str_replace('@', '[at]', $email); $safe_display_email = str_replace('.', '[dot]', $email);
How to remove html special chars from string in php
htmlentities php function is ued to removr html special chars from a string in php.
$orig = "I'll \"walk\" the <b>dog</b> now"; $a = htmlentities($orig); $b = html_entity_decode($a); echo $a; // I'll "walk" the <b>dog</b> now echo $b; // I'll "walk" the <b>dog</b> now
Remove special characters from string except space in php
Replace all characters except letters, numbers, spaces and underscores. preg_replace php function is used to remove special characters from a string except space in php.
$string = preg_replace("/[^ \w]+/", "", $string);
Remove specific word from string in php
Delete particular word from string using php str_replace function.
$string = str_replace('Facebook', 'FB', $string);