Home >Backend Development >PHP Tutorial >php handles strings

php handles strings

WBOY
WBOYOriginal
2016-07-29 09:00:08726browse

1. Get the string length

<code><span>$str</span> = <span>'abc'</span>;
<span>$str1</span>=<span>'中文'</span>;
<span>echo</span> strlen(<span>$str</span>),<span>'<br/>'</span>,strlen(<span>$str1</span>); 

输出结果:
<span>3</span><span>6</span></code>

2. Find the string

<code>//查找字符串首次出现的位置
<span>$char</span> = <span>'a'</span>;
<span>$str</span> = <span>'abc'</span>;
<span>$bRet</span> = strpos(<span>$str</span>,<span>$char</span>);

<span>if</span>(<span>$bRet</span> === <span>false</span>)
{
    <span>echo</span><span>"<span>$str</span> 中不含有<span>$char</span> "</span>;
}<span>else</span>
{
    <span>echo</span><span>"<span>$str</span> 中含有<span>$char</span> "</span>;
}
<span>echo</span><span>'<br />'</span>;

输出结果:
abc 中含有a </code>

3. Replace the string

<code><span>//替换字符串str_replace()</span><span>$str</span> = <span>'Good morning'</span>;
<span>$str1</span> = str_replace(<span>'moring'</span>,<span>'afternoon'</span>,<span>$str</span>);
<span>echo</span><span>$str</span>, <span>'<br/>'</span>, <span>$str1</span>;

输出结果:
Good morning
Good morning

<span>//替换字符串strtr()</span><span>$str</span> = <span>'上车,上课,上班,下车,下课,下班'</span>;
<span>$str1</span> = strtr(<span>$str</span>,[<span>'上'</span>=><span>'下'</span>,<span>'下'</span>=><span>'上'</span>]);
<span>echo</span><span>$str</span>,<span>'<br / >'</span>,<span>$str1</span>;

输出结果:
上车,上课,上班,下车,下课,下班
下车,下课,下班,上车,上课,上班</code>

4. Intercept the string

<code><span>//截取字符串substr(),参数分别为目标字符串,起始位置,截取长度</span><span>$str</span> = <span>'East,west,home is best'</span>;
<span>echo</span><span>$str</span>,<span>'<br/>'</span>,substr(<span>$str</span>,<span>0</span>,<span>4</span>);

输出结果:
East,west,home is best
East</code>

5. Split the string

<code><span>//拆分字符串explode()</span><span>$str</span> = <span>'厨子, 戏子, 痞子'</span>;
<span>$arr</span> = explode(<span>','</span>,<span>$str</span>);
<span>echo</span><span>$str</span>,<span>'<br/>'</span>;
print_r(<span>$arr</span>);

输出结果:
厨子, 戏子, 痞子
<span>Array</span> ( [<span>0</span>] => 厨子 [<span>1</span>] => 戏子 [<span>2</span>] => 痞子 )</code>

6. Combine the arrays into characters string

<code><span>$arr</span>=[<span>'0'</span>=><span>'厨子'</span>,<span>'1'</span>=><span>'戏子'</span>,<span>'2'</span>=><span>'痞子'</span>];
<span>$str</span>=implode(<span>$arr</span>,<span>';'</span>);
print_r(<span>$arr</span>);
<span>echo</span><span>'<br/>'</span>;
<span>echo</span><span>$str</span>;

输出结果:
<span>Array</span> ( [<span>0</span>] => 厨子 [<span>1</span>] => 戏子 [<span>2</span>] => 痞子 ) 
厨子;戏子;痞子</code>
').addClass('pre-numbering').hide(); $(this).addClass('has-numbering').parent().append($numbering); for (i = 1; i ').text(i)); }; $numbering.fadeIn(1700); }); });

The above introduces the processing of strings in PHP, including aspects of the content. I hope it will be helpful to friends who are interested in PHP tutorials.

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