How to replace string in array in php

PHPz
Release: 2023-04-23 09:27:33
Original
573 people have browsed it

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.

  1. str_replace() function

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)
Copy after login

Where:

  • search: The string or character array that needs to be found and replaced.
  • replace: Used to replace the found string or character array.
  • subject: The string or string array that needs to be replaced.

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;
Copy after login

The output result is:

"hello_world"
Copy after login

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.

  1. Replace strings in arrays

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");
Copy after login

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);
Copy after login

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.

  1. Replace multiple strings

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.";
Copy after login

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;
Copy after login

The output result is:

"Hello, Java is a easy programming language, and can be used to develop various applications."
Copy after login

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.

  1. Conclusion

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!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!