Heim  >  Artikel  >  Backend-Entwicklung  >  php实现子字符串位置相互对调互换的方法 原创,fontcolor_PHP教程

php实现子字符串位置相互对调互换的方法 原创,fontcolor_PHP教程

WBOY
WBOYOriginal
2016-07-12 08:51:02835Durchsuche

php实现子字符串位置相互对调互换的方法 原创,fontcolor

本文实例讲述了php实现子字符串位置相互对调互换的方法。分享给大家供大家参考,具体如下:

<&#63;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);
&#63;>

运行结果为:

This is an example,you see Jack tell Tom something

补充:小编在这里推荐一款本站的php格式化美化的排版工具帮助大家在以后的PHP程序设计中进行代码排版:

php代码在线格式化美化工具:

http://tools.jb51.net/code/phpformat

另外,由于php属于C语言风格,因此下面这款工具同样可以实现php代码的格式化:

C语言风格/HTML/CSS/json代码格式化美化工具:
http://tools.jb51.net/code/ccode_html_css_json

更多关于PHP相关内容感兴趣的读者可查看本站专题:《PHP数学运算技巧总结》、《php操作office文档技巧总结(包括word,excel,access,ppt)》、《PHP数组(Array)操作技巧大全》、《php排序算法总结》、《PHP常用遍历算法与技巧总结》、《PHP数据结构与算法教程》、《php程序设计算法总结》、《php正则表达式用法总结》、《PHP运算与运算符用法总结》、《php字符串(string)用法总结》及《php常见数据库操作技巧汇总》

希望本文所述对大家PHP程序设计有所帮助。

www.bkjia.comtruehttp://www.bkjia.com/PHPjc/1133029.htmlTechArticlephp实现子字符串位置相互对调互换的方法 原创,fontcolor 本文实例讲述了php实现子字符串位置相互对调互换的方法。分享给大家供大家参考...
Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn