How to remove specific characters from a string in PHP

1 Answer

0 votes
function removeSpecificCharactersFromString($str, $charsToRemove) {
    foreach ($charsToRemove as $ch) {
        $str = str_replace($ch, "", $str);
    }
    
    return $str;
}

$phone = "(555) 555-5555";
$charsToRemove = array('(', ')', '-');

$phone = removeSpecificCharactersFromString($phone, $charsToRemove);

echo $phone;



/*
run:

555 5555555

*/

 



answered Mar 17, 2024 by avibootz

Related questions

1 answer 152 views
2 answers 208 views
2 answers 142 views
1 answer 127 views
1 answer 147 views
1 answer 122 views
...