How does PHP implement sub-string locations to swap with each other? This article mainly introduces the PHP implementation of sub-string location exchange between the method, can achieve a simple string of two sub-string interchange function, involving the PHP string operation and insertion, replacement and other operations related skills, the need for friends can refer to. We hope to help you.
Specific as follows:
<?php/* Sub-string position Interchange */$str 1= "Tom"; $str 2= "Jack"; $str = "This is a example,you see Tom tell Jack something"; function str_ Change ($str, $str 1, $str 2) { $len 1=strlen ($str 1); $len 2=strlen ($str 2); $pos 1=strpos ($str, $str 1); $str =substr_replace ($str, $str 2, $pos 1, $len 1);//replace $str1 for $STR2 $pos 2= strpos ($str, $str 2, $len 1+ $pos 1);// Locate the position of the $STR2 field after the replacement string return Substr_replace ($str, $str 1, $pos 2, $len 2);//replace $STR2 for $str1}echo Str_change ($STR, $ STR1, $str 2);? >
The result of the operation is:
This is a example,you see Jack tell Tom something
Related recommendations:
Related functions for PHP string escaping
Introduction to PHP string segmentation and comparison
PHP string Regular replacement function Preg_replace using the detailed