Home > headlines > PHP string functions (6): search and replace

PHP string functions (6): search and replace

无忌哥哥
Release: 2018-06-28 10:53:00
Original
3317 people have browsed it

* 1.strpos($str1,$str2, $offset) finds the position where $str1 first appears in $str1

* 2.strstr($str1, $str2), if $str2 is The substring of $str1, returns the substring, otherwise returns false

* If it is determined that $str2 is the substring of $str1, it is recommended to use strpos(), which is faster

* 3.str_replace ($str1, $str2, $str3, $num): substring replacement, $num is the number of replacements

* 4.substr_replace($str1,$str2,$str3,$start, $length): Replace the substring of the string

$str = 'www.php.cn';
Copy after login

//1.strpos($str,$needle, $offset)Find the first occurrence of the string

echo strpos($str, &#39;p&#39;),&#39;<br>&#39;; //默认从头开始查找
echo strpos($str, &#39;p&#39;, 5),&#39;<br>&#39;; //从索引5开始查找
Copy after login

//2.strstr($str1 , $str2), if $str2 is a substring of $str1, return the substring, otherwise return false

echo strstr($str, &#39;php&#39;),&#39;<br>&#39;;  //返回子串及后面部分
echo strstr($str, &#39;php&#39;, true),&#39;<br>&#39;; //参数true,会返回子串前面部分
echo &#39;<hr>&#39;;
Copy after login

//3.str_replace($str1, $str2, $str3, $num): substring Replace,

echo str_replace(&#39;www&#39;,&#39;http://www&#39;,$str), &#39;<br>&#39;;
echo &#39;<hr>&#39;;
Copy after login

//4.substr_replace($str1,$str2,$str3,$start, $length): Replace the substring of the string

//In $str, The 2 characters starting from the 5th index position are replaced with 'pppph'

echo substr_replace($str,&#39;pppph&#39;, 5, 2);
Copy after login
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template