Home > Backend Development > PHP Tutorial > php string function (3)

php string function (3)

WBOY
Release: 2016-08-08 09:24:05
Original
1066 people have browsed it

Seven: String interception function: str_replace(find,replace,string,count);
substr_replace(string,replace,start,length);

<code><span>$msg</span> = <span>"hello,word I love php"</span>;
<span>$rs</span> = substr_replace(<span>$msg</span>,<span>"mysql"</span>,-<span>3</span>,<span>3</span>);
<span>echo</span><span>$rs</span>.<span>"<br/>"</span>;
<span>$rsl</span> = str_replace(<span>"word"</span>, <span>"php"</span>, <span>$msg</span>);
<span>echo</span><span>$rsl</span>;</code>
Copy after login

As shown in the figure below, substr_replace(string,replace,start,length) ; Mainly for replacement of positions in strings. string is
The searched string, replace is the character to be replaced, start is the starting position of replacement (if it is a positive number, search from the left. If it is a negative number, search from the right), length (optional. If not selected, then Indicates replacing all characters after the starting position) indicates the length to be replaced.

str_replace(find,replace,string,count); find represents the character to be replaced. replace represents the character to be replaced. string represents the string to be found. count represents the number of executions (optional). This function is case sensitive. Case-insensitive str_ireplace(); usage is the same as str_replace().


8

str2). strcasecmp(str1,str2);

<code><span>$msg1</span> = <span>"hello"</span>;
    <span>$msg2</span> = <span>"HELLO"</span>;
    <span>echo</span> strcmp(<span>$msg1</span>, <span>$msg2</span>).<span>"<br/>"</span>;
    <span>echo</span> strcasecmp(<span>$msg1</span> ,<span>$msg2</span>);</code>
Copy after login
The result is as shown below. The difference between the two functions is that strcmp() is lower case sensitive, while strcasecmp() is not case sensitive. When the compared characters are the same, the return value is 0. When str1 > str2, the return value is greater than 0. When str1 < str2, the return value is less than 0. 9: String case conversion strtolower (); strtoupper (); ucfirst(); ucwords();
<code><span>$str</span> = <span>"I AM PETAL"</span>;
<span>echo</span> strtolower(<span>$str</span>).<span>"<br/>"</span>;  <span>//大写转换为小写</span><span>$stra</span> = <span>"i am petal"</span>;
<span>echo</span> strtoupper(<span>$stra</span>).<span>"<br/>"</span>; <span>// 小卫转换为大写</span><span>echo</span> ucfirst(<span>$stra</span>).<span>"<br/>"</span>;  <span>//只将字符串的第一个字符转换为大写</span><span>echo</span> ucwords(<span>$stra</span>);       <span>//将字符串每一个单词的首字母转换为大写</span></code>
Copy after login
The results are as follows The above introduces the PHP string function (3), including the relevant content. I hope it will be helpful to friends who are interested in PHP tutorials.

Related labels:
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