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

yipeiwu_com6年前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代码在线格式化美化工具:

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程序设计有所帮助。

相关文章

PHP运行出现Notice : Use of undefined constant 的完美解决方案分享

Notice: Use of undefined constant ALL_PS - assumed 'ALL_PS' in E:\Server\vhosts\www.lvtao.net...

PHP curl_setopt()函数实例代码与参数分析

curl_setopt (PHP 4 >= 4.0.2) curl_setopt -- 为CURL调用设置一个选项 描述   bool curl_setopt (int ch, s...

Linux系统下php获得系统分区信息的方法

本文实例讲述了Linux系统下php获得系统分区信息的方法。分享给大家供大家参考。具体实现方法如下: $pars = array_filter(explode("\n",`df -h...

php无限级分类实现方法分析

本文实例讲述了php无限级分类实现方法。分享给大家供大家参考,具体如下: 1. 递归 public function getInfo(){ $data=$this->selec...

php 静态化实现代码

模板文件template.htm: 复制代码 代码如下:<html> <head> <title>%title%</title> <...