In PHP development, string replacement operations are often required. Normally, we use the str_replace() function to replace strings. However, when there are a lot of strings to be replaced, we can also perform batch replacement through arrays. In this article, we will explain in detail how to replace strings in arrays in PHP.
str_replace() function is a built-in string replacement function in PHP, which can be used to replace certain characters or strings in a string . The syntax is as follows:
str_replace(search, replace, subject)
Where:
For example, we can use the str_replace() function to replace all spaces in the string with underscores. The code is as follows:
$str = "hello world"; $new_str = str_replace(" ", "_", $str); echo $new_str;
The output result is:
"hello_world"
However, when we need to replace a large number of strings, using the str_replace() function is not convenient enough. Therefore, we can use arrays to perform replacement operations.
Suppose we have an array that contains some strings, and we need to replace some of them. To achieve this, we can use the array_map() function and str_replace() function in PHP.
The array_map() function passes each corresponding element of one or more arrays as a parameter to the specified callback function and returns a new array whose elements are the return values of the callback function. The str_replace() function is used to replace all strings found in the array.
For example, we have the following string array:
$arr = array("hello world", "my name is php", "i love coding");
We want to replace all "php" in it with "Java", you can use the following code:
$new_arr = array_map(function($val){ return str_replace("php", "Java", $val); }, $arr);
Above The code first defines a callback function to implement the replacement operation, and then applies the callback function to each array element through the array_map() function. The final $new_arr array contains the replaced new string.
If there are multiple strings that need to be replaced, we can store the string to be replaced and the replaced string in an array , and then use the str_replace() function to implement batch replacement.
For example, we have the following string:
$str = "Hello, php is a powerful programming language, and can be used to develop various applications.";
We need to replace "php" with "Java" and "powerful" with "easy", you can use the following code:
$search = array("php", "powerful"); $replace = array("Java", "easy"); $new_str = str_replace($search, $replace, $str); echo $new_str;
The output result is:
"Hello, Java is a easy programming language, and can be used to develop various applications."
In the above code, we first define two arrays $search and $replace to store the string to be replaced and the replaced string respectively. Then use the str_replace() function to replace all strings in the $search array with corresponding strings in the $replace array.
In PHP development, string replacement is a very common operation. Through the introduction of this article, we have learned how to use the str_reaplce() function and arrays to perform replacement operations. Especially when batch replacement is required, it is more convenient to use array replacement. At the same time, we also learned how to use the array_map() function and str_replace() function to replace strings in the array. These techniques are very practical in actual development and I hope they will be helpful to readers.
The above is the detailed content of How to replace string in array in php. For more information, please follow other related articles on the PHP Chinese website!