I have data like this in my array first_name, last_name,....
I am looking for an array to replace the _ with a space..is this possible?
I took alook at http://php.net/manual/en/function.array-replace.php but I am not sure if this is what I want.
Approach 1: Using the str_replace() and str_split() functions in PHP. The str_replace() function is used to replace multiple characters in a string and it takes in three parameters. The first parameter is the array of characters to replace.
To replace an element in an array: Use the indexOf() method to get the index of the element you want to replace. Call the Array. splice() method to replace the element at the specific index. The array element will get replaced in place.
PHP | preg_replace() Function The preg_replace() function is an inbuilt function in PHP which is used to perform a regular expression for search and replace the content.
Answer: Use the PHP str_replace() function You can use the PHP str_replace() function to replace all the occurrences of a word within a string.
Just with str_replace
, it takes either strings or arrays in all arguments that matter:
var_dump(str_replace('_',' ',array('foo_bar','lorem_ipsum')));
array(2) {
[0]=>
string(7) "foo bar"
[1]=>
string(11) "lorem ipsum"
}
foreach($array as $key=>$value){
$array[$key]=str_replace("_"," ",$value);
}
That should do it, right?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With