string replace in php
<?php
// This is the string we will search through
$stringToSearch = "Hello World";
// This is the string we are trying to find
$stringWeAreLookingFor = "World";
// If we find the string we are looking for, we will replace
// it with this string
$replaceString = "PHP";
// We use the str_replace method to replace a substring in php
$newString = str_replace( $stringWeAreLookingFor, $replaceString, $stringToSearch );
// The newString variable now contains the string "Hello PHP"
// Please note this search IS case sensitive. Use str_ireplace()
// case in-sensitive searches.
// This function can also be used with an array of strings
$stringArray = [ "Test", "Test", "Goose" ];
$stringWeAreLookingFor = "Test";
$replaceString = "Duck";
$newArray = str_replace( $stringWeAreLookingFor, $replaceString, $stringArray );
// The newArray variable will now contain [ "Duck", "Duck", "Goose" ]
?>