This is a method I found on the Internet. It is very good. Its function is: when string A appears in string B multiple times, it is used when we need to replace the first string A in string B with another string. For example, we need to use it to add links to keywords in the article content.
$needle=>String A,$replace=>The new string to replace string A with,$haystack=>String B;
Usage example: echo str_replace_once("Xiancheng.com","Xiancheng.com","Xiancheng.com—Xiancheng CSM official website");
function str_replace_once($needle, $replace, $haystack) {//只替换一次字符串
$pos = strpos($haystack, $needle);
if ($pos === false) {
return $haystack;
}
return substr_replace($haystack, $replace, $pos, strlen($needle));
}