Home  >  Article  >  Backend Development  >  How to swap substring positions in PHP Original, fontcolor_PHP tutorial

How to swap substring positions in PHP Original, fontcolor_PHP tutorial

WBOY
WBOYOriginal
2016-07-12 08:51:02866browse

PHP’s method of swapping substring positions. Original, fontcolor

The example in this article describes how PHP can swap substring positions. Share it with everyone for your reference, the details are as follows:

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

The running result is:

This is an example, you see Jack tell Tom something

Supplement: The editor here recommends a PHP formatting and beautifying typesetting tool on this website to help you code typesetting in future PHP programming:

php code online formatting and beautification tool:

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

In addition, since php belongs to the C language style, the following tool can also format php code:

C language style/HTML/CSS/json code formatting and beautification tool:
http://tools.jb51.net/code/ccode_html_css_json

Readers who are interested in more PHP-related content can check out the special topics on this site: "Summary of PHP mathematical operation skills", "Summary of PHP operating office document skills (including word, excel, access, ppt)", "PHP array ( Array) operating skills collection", "php sorting algorithm summary", "php common traversal algorithms and techniques summary", "php data structure and algorithm tutorial", "php programming algorithm summary", "php regular expression usage summary", "Summary of PHP operations and operator usage", "Summary of PHP string usage" and "Summary of common PHP database operation skills"

I hope this article will be helpful to everyone in PHP programming.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/1133029.htmlTechArticlephp method to realize substring position swapping and exchange original, fontcolor This article tells the example of php realizing substring A method of swapping positions. Share it with everyone for your reference...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn