How PHP implements sub-string position swapping with each other

Source: Internet
Author: User
How does PHP implement sub-string locations to swap with each other? This article mainly introduces the PHP implementation of sub-string location exchange between the method, can achieve a simple string of two sub-string interchange function, involving the PHP string operation and insertion, replacement and other operations related skills, the need for friends can refer to. We hope to help you.

Specific as follows:

<?php/* Sub-string position Interchange */$str 1= "Tom"; $str 2= "Jack"; $str = "This is a example,you see Tom tell Jack something"; function str_ Change ($str, $str 1, $str 2) {  $len 1=strlen ($str 1);  $len 2=strlen ($str 2);  $pos 1=strpos ($str, $str 1);  $str =substr_replace ($str, $str 2, $pos 1, $len 1);//replace $str1 for $STR2  $pos 2= strpos ($str, $str 2, $len 1+ $pos 1);// Locate the position of the $STR2 field after the replacement string  return Substr_replace ($str, $str 1, $pos 2, $len 2);//replace $STR2 for $str1}echo Str_change ($STR, $ STR1, $str 2);? >

The result of the operation is:

This is a example,you see Jack tell Tom something

Related recommendations:

Related functions for PHP string escaping

Introduction to PHP string segmentation and comparison

PHP string Regular replacement function Preg_replace using the detailed

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.