您好,欢迎访问一九零五行业门户网

php如何实现子字符串位置相互对调互换

php如何实现子字符串位置相互对调互换?本文主要介绍了php实现子字符串位置相互对调互换的方法,可实现简单字符串中两个子字符串互换的功能,涉及php字符串运算与插入、替换等操作的相关技巧,需要的朋友可以参考下。希望对大家有所帮助。
具体如下:
<?php /*子字符串位置互换 */ $str1="tom"; $str2="jack"; $str="this is an example,you see tom tell jack something"; function str_change($str,$str1,$str2){ $len1=strlen($str1); $len2=strlen($str2); $pos1=strpos($str,$str1); $str=substr_replace($str,$str2,$pos1,$len1);//替换$str1为$str2 $pos2= strpos($str,$str2,$len1+$pos1);//定位替换后字符串中原$str2字段的位置 return substr_replace($str,$str1,$pos2,$len2);//替换$str2为$str1 } echo str_change($str,$str1,$str2); ?>
运行结果为:
this is an example,you see jack tell tom something
相关推荐:
php 字符串转义的相关函数
php 字符串分割和比较介绍
php 字符串正则替换函数preg_replace使用详解
以上就是php如何实现子字符串位置相互对调互换的详细内容。
其它类似信息

推荐信息