How to replace strings in array in php

王林
Release: 2023-03-08 12:42:02
Original
3325 people have browsed it

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

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

(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!

Related labels:
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