PHP str_replace() Function

P
PHP str_repeat() Function
PHP strcasecmp() Function

what is the syntax of the STR_REPLACE() function in php?

str_replace(find,replace,string,count)
ParameterDescription
findRequired. Specifies the value to find
replaceRequired. Specifies the value to replace the value in find
stringRequired. Specifies the string to be searched
countOptional. A variable that counts the number of replacements
PHP STR_REPLACE() method

examples of the STR_REPLACE() function

Example 1. In this example, we replace the characters “world” in the string “Hello world!” with “Peter”.

<?php
echo str_replace("world","Peter","Hello world!");
?>

Example 2. In this example, we use str_replace() with an array and a count variable.

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

Example 3. In this example, we use str_replace() with fewer elements in replace than find.

<?php
$find = array("Hello","world");
$replace = array("B");
$arr = array("Hello","world","!");
print_r(str_replace($find,$replace,$arr));
?>
PHP str_repeat() Function
PHP strcasecmp() Function

Add comment

Tutor Network

Share this

Learn PHP from A to Z