PHP – Replace Multiple Characters

To replace multiple characters in PHP, here is a simple solution:

Replace Multiple Characters PHP

If you want to replace more than once characters in a string or an array, like you have an array of phone number or address, where you want to remove more than one values from that array, for example, dashes ( – ) and slashes (/), then you can use the still use the str_replace function, where the first parameter will be an array for removing of the defined characters, with the second argument as array, from where you want to remove those characters. Here is an example

<?php
$arr = array(“M/U/L/T/I/P/L/E”, “Charac-ters RE-PLACE/D”);

$arr_remove = array(“-“,”/”);

$new_arr = str_replace($arr_remove, “”, $arr);

print_r($new_arr);
?>

The $new_arr array will now look like below:

Array

(

[0] => MULTIPLE

[1] => Characters REPLACED

)

Uncategorised

Leave a Reply

Your email address will not be published. Required fields are marked *