Maison > développement back-end > tutoriel php > php处理字符串

php处理字符串

WBOY
Libérer: 2016-07-29 09:00:08
original
721 Les gens l'ont consulté

1、获取字符串长度

<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>
Copier après la connexion

2、查找字符串

<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>
Copier après la connexion

3、替换字符串

<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>
Copier après la connexion

4、截取字符串

<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>
Copier après la connexion

5、拆分字符串

<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>
Copier après la connexion

6、数组合并为字符串

<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>
Copier après la connexion
').addClass('pre-numbering').hide(); $(this).addClass('has-numbering').parent().append($numbering); for (i = 1; i ').text(i)); }; $numbering.fadeIn(1700); }); });

以上就介绍了php处理字符串,包括了方面的内容,希望对PHP教程有兴趣的朋友有所帮助。

Étiquettes associées:
source:php.cn
Déclaration de ce site Web
Le contenu de cet article est volontairement contribué par les internautes et les droits d'auteur appartiennent à l'auteur original. Ce site n'assume aucune responsabilité légale correspondante. Si vous trouvez un contenu suspecté de plagiat ou de contrefaçon, veuillez contacter admin@php.cn
Tutoriels populaires
Plus>
Derniers téléchargements
Plus>
effets Web
Code source du site Web
Matériel du site Web
Modèle frontal