本文实例讲述了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
[b]补充:小编在这里推荐一款本站的php格式化美化的排版工具帮助大家在以后的PHP程序设计中进行代码排版:
[/b]
[b]
php代码在线格式化美化工具:[/b]
[url=http://tools.jb51.net/code/phpformat]http://tools.jb51.net/code/phpformat[/url]
[b]另外,由于php属于C语言风格,因此下面这款工具同样可以实现php代码的格式化:[/b]
[b]C语言风格/HTML/CSS/json代码格式化美化工具:
[/b][url=http://tools.jb51.net/code/ccode_html_css_json]http://tools.jb51.net/code/ccode_html_css_json[/url]
更多关于PHP相关内容感兴趣的读者可查看本站专题:《[url=http://www.1sucai.cn/Special/630.htm]PHP数学运算技巧总结[/url]》、《[url=http://www.1sucai.cn/Special/339.htm]php操作office文档技巧总结(包括word,excel,access,ppt)[/url]》、《[url=http://www.1sucai.cn/Special/623.htm]PHP数组(Array)操作技巧大全[/url]》、《[url=http://www.1sucai.cn/Special/150.htm]php排序算法总结[/url]》、《[url=http://www.1sucai.cn/Special/242.htm]PHP常用遍历算法与技巧总结[/url]》、《[url=http://www.1sucai.cn/Special/614.htm]PHP数据结构与算法教程[/url]》、《[url=http://www.1sucai.cn/Special/111.htm]php程序设计算法总结[/url]》、《[url=http://www.1sucai.cn/Special/180.htm]php正则表达式用法总结[/url]》、《[url=http://www.1sucai.cn/Special/357.htm]PHP运算与运算符用法总结[/url]》、《[url=http://www.1sucai.cn/Special/47.htm]php字符串(string)用法总结[/url]》及《[url=http://www.1sucai.cn/Special/231.htm]php常见数据库操作技巧汇总[/url]》
希望本文所述对大家PHP程序设计有所帮助。