Home  >  Article  >  Backend Development  >  How to replace strings in array in php

How to replace strings in array in php

王林
王林Original
2021-03-04 17:35:403261browse

How to replace strings in an array in php: You can use the str_replace function to replace, such as [str_replace("red","pink",$arr,$i)]. If the string being searched is an array, then it will find and replace each element in the array.

How to replace strings in array in php

The operating environment of this article: windows10 system, php 7.3, thinkpad t480 computer.

If we need to replace strings in the array, we can use the str_replace() function.

Tip: If the searched string is an array, then it will find and replace each element in the array.

Syntax:

str_replace(find,replace,string,count)

Parameter introduction:

  • find Required. Specifies the value to look for.

  • replace Required. Specifies the value to replace the value in find .

  • #string Required. Specifies the string to be searched for.

  • #count Optional. A variable counting the number of substitutions.

Code example:

<!DOCTYPE html>
<html>
<body>

<?php
$arr = array("blue","red","green","yellow");
print_r(str_replace("red","pink",$arr,$i));
echo "<br>" . "Replacements: $i";
?>

<p>In this example, we search an array to find the value "red", and then we replace the value "red" with "pink".</p>

</body>
</html>

(Learning video sharing: php video tutorial)

Running results:

How to replace strings in array in php

Related recommendations: php tutorial

The above is the detailed content of How to replace strings in array in php. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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