php如何?子字串位置相互對調互換

來源:互聯網
上載者:User
php如何?子字串位置相互對調互換?本文主要介紹了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 字串正則替換函數preg_replace使用詳解

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.