Maison > php教程 > php手册 > 常用字符串处理代码片段

常用字符串处理代码片段

WBOY
Libérer: 2016-06-21 08:53:01
original
891 Les gens l'ont consulté

  移除 HTML 标签

<ol class="dp-c"><li class="alt"><span><span class="vars">$text</span><span> = </span><span class="func">strip_tags</span><span>(</span><span class="vars">$input</span><span>, </span><span class="string">""</span><span>); </span></span></li></ol>
Copier après la connexion

  返回 $start 和 $end 之间的文本

<ol class="dp-c">
<li class="alt"><span><span class="keyword">function</span><span> GetBetween(</span><span class="vars">$content</span><span>,</span><span class="vars">$start</span><span>,</span><span class="vars">$end</span><span>){ </span></span></li>
<li><span>    <span class="vars">$r</span><span> = </span><span class="func">explode</span><span>(</span><span class="vars">$start</span><span>, </span><span class="vars">$content</span><span>); </span></span></li>
<li class="alt"><span>    <span class="keyword">if</span><span> (isset(</span><span class="vars">$r</span><span>[1])){ </span></span></li>
<li><span>        <span class="vars">$r</span><span> = </span><span class="func">explode</span><span>(</span><span class="vars">$end</span><span>, </span><span class="vars">$r</span><span>[1]); </span></span></li>
<li class="alt"><span>        <span class="keyword">return</span><span> </span><span class="vars">$r</span><span>[0]; </span></span></li>
<li><span>    } </span></li>
<li class="alt"><span>    <span class="keyword">return</span><span> </span><span class="string">''</span><span>; </span></span></li>
<li><span>} </span></li>
</ol>
Copier après la connexion

  将url转换成链接

<ol class="dp-c">
<li class="alt"><span><span class="vars">$url</span><span> = </span><span class="string">"Jean-Baptiste Jung (http://www.webdevcat.com)"</span><span>; </span></span></li>
<li><span><span class="vars">$url</span><span> = preg_replace(</span><span class="string">"#http://([A-z0-9./-]+)#"</span><span>, </span><span class="string">'<a>$0</a>'</span><span>, </span><span class="vars">$url</span><span>); </span></span></li>
</ol>
Copier après la connexion

  切分字符串为140个字符

<ol class="dp-c">
<li class="alt"><span><span class="keyword">function</span><span> split_to_chunks(</span><span class="vars">$to</span><span>,</span><span class="vars">$text</span><span>){ </span></span></li>
<li><span>    <span class="vars">$total_length</span><span> = (140 - </span><span class="func">strlen</span><span>(</span><span class="vars">$to</span><span>)); </span></span></li>
<li class="alt"><span>    <span class="vars">$text_arr</span><span> = </span><span class="func">explode</span><span>(</span><span class="string">" "</span><span>,</span><span class="vars">$text</span><span>); </span></span></li>
<li><span>    <span class="vars">$i</span><span>=0; </span></span></li>
<li class="alt"><span>    <span class="vars">$message</span><span>[0]=</span><span class="string">""</span><span>; </span></span></li>
<li><span>    <span class="keyword">foreach</span><span> (</span><span class="vars">$text_arr</span><span> </span><span class="keyword">as</span><span> </span><span class="vars">$word</span><span>){ </span></span></li>
<li class="alt"><span>        <span class="keyword">if</span><span> ( </span><span class="func">strlen</span><span>(</span><span class="vars">$message</span><span>[</span><span class="vars">$i</span><span>] . </span><span class="vars">$word</span><span> . </span><span class="string">' '</span><span>) <span class="vars">$total_length</span><span> ){ </span></span></span></li>
<li><span>            <span class="keyword">if</span><span> (</span><span class="vars">$text_arr</span><span>[</span><span class="func">count</span><span>(</span><span class="vars">$text_arr</span><span>)-1] == </span><span class="vars">$word</span><span>){ </span></span></li>
<li class="alt"><span>                <span class="vars">$message</span><span>[</span><span class="vars">$i</span><span>] .= </span><span class="vars">$word</span><span>; </span></span></li>
<li><span>            } <span class="keyword">else</span><span> { </span></span></li>
<li class="alt"><span>                <span class="vars">$message</span><span>[</span><span class="vars">$i</span><span>] .= </span><span class="vars">$word</span><span> . </span><span class="string">' '</span><span>; </span></span></li>
<li><span>            } </span></li>
<li class="alt"><span>        } <span class="keyword">else</span><span> { </span></span></li>
<li><span>            <span class="vars">$i</span><span>++; </span></span></li>
<li class="alt"><span>            <span class="keyword">if</span><span> (</span><span class="vars">$text_arr</span><span>[</span><span class="func">count</span><span>(</span><span class="vars">$text_arr</span><span>)-1] == </span><span class="vars">$word</span><span>){ </span></span></li>
<li><span>                <span class="vars">$message</span><span>[</span><span class="vars">$i</span><span>] = </span><span class="vars">$word</span><span>; </span></span></li>
<li class="alt"><span>            } <span class="keyword">else</span><span> { </span></span></li>
<li><span>                <span class="vars">$message</span><span>[</span><span class="vars">$i</span><span>] = </span><span class="vars">$word</span><span> . </span><span class="string">' '</span><span>; </span></span></li>
<li class="alt"><span>            } </span></li>
<li><span>        } </span></li>
<li class="alt"><span>    } </span></li>
<li><span>    <span class="keyword">return</span><span> </span><span class="vars">$message</span><span>; </span></span></li>
<li class="alt"><span>} </span></li>
</ol>
Copier après la connexion

  删除字符串中的URL

<ol class="dp-c"><li class="alt"><span><span class="vars">$string</span><span> = preg_replace(</span><span class="string">'/\b(https?ftpfile):\/\/[-A-Z0-9+&@#\/%?=~_$!:,.;]*[A-Z0-9+&@#\/%=~_$]/i'</span><span>, </span><span class="string">''</span><span>, </span><span class="vars">$string</span><span>); </span></span></li></ol>
Copier après la connexion

  将字符串转成SEO友好的字符串

<ol class="dp-c">
<li class="alt"><span><span class="keyword">function</span><span> slug(</span><span class="vars">$str</span><span>){ </span></span></li>
<li><span>    <span class="vars">$str</span><span> = </span><span class="func">strtolower</span><span>(trim(</span><span class="vars">$str</span><span>)); </span></span></li>
<li class="alt"><span>    <span class="vars">$str</span><span> = preg_replace(</span><span class="string">'/[^a-z0-9-]/'</span><span>, </span><span class="string">'-'</span><span>, </span><span class="vars">$str</span><span>); </span></span></li>
<li><span>    <span class="vars">$str</span><span> = preg_replace(</span><span class="string">'/-+/'</span><span>, </span><span class="string">"-"</span><span>, </span><span class="vars">$str</span><span>); </span></span></li>
<li class="alt"><span>    <span class="keyword">return</span><span> </span><span class="vars">$str</span><span>; </span></span></li>
<li><span>} </span></li>
</ol>
Copier après la connexion

  解析 CSV 文件

<ol class="dp-c">
<li class="alt"><span><span class="vars">$fh</span><span> = </span><span class="func">fopen</span><span>(</span><span class="string">"contacts.csv"</span><span>, </span><span class="string">"r"</span><span>); </span></span></li>
<li><span><span class="keyword">while</span><span>(</span><span class="vars">$line</span><span> = </span><span class="func">fgetcsv</span><span>(</span><span class="vars">$fh</span><span>, 1000, </span><span class="string">","</span><span>)) { </span></span></li>
<li class="alt"><span>    <span class="func">echo</span><span> </span><span class="string">"Contact: {$line[1]}"</span><span>; </span></span></li>
<li><span>} </span></li>
</ol>
Copier après la connexion

  字符串搜索

<ol class="dp-c">
<li class="alt"><span><span class="keyword">function</span><span> contains(</span><span class="vars">$str</span><span>, </span><span class="vars">$content</span><span>, </span><span class="vars">$ignorecase</span><span>=true){ </span></span></li>
<li><span>    <span class="keyword">if</span><span> (</span><span class="vars">$ignorecase</span><span>){ </span></span></li>
<li class="alt"><span>        <span class="vars">$str</span><span> = </span><span class="func">strtolower</span><span>(</span><span class="vars">$str</span><span>); </span></span></li>
<li><span>        <span class="vars">$content</span><span> = </span><span class="func">strtolower</span><span>(</span><span class="vars">$content</span><span>); </span></span></li>
<li class="alt"><span>    } </span></li>
<li><span>    <span class="keyword">return</span><span> </span><span class="func">strpos</span><span>(</span><span class="vars">$content</span><span>,</span><span class="vars">$str</span><span>) ? true : false; </span></span></li>
<li class="alt"><span>} </span></li>
</ol>
Copier après la connexion

  检查字符串是否以某个串开始

<ol class="dp-c">
<li class="alt"><span><span class="keyword">function</span><span> String_Begins_With(</span><span class="vars">$needle</span><span>, </span><span class="vars">$haystack</span><span> { </span></span></li>
<li><span>    <span class="keyword">return</span><span> (</span><span class="func">substr</span><span>(</span><span class="vars">$haystack</span><span>, 0, </span><span class="func">strlen</span><span>(</span><span class="vars">$needle</span><span>))==</span><span class="vars">$needle</span><span>); </span></span></li>
<li class="alt"><span>} </span></li>
</ol>
Copier après la connexion

  从字符串中提取email地址

<ol class="dp-c">
<li class="alt"><span><span class="keyword">function</span><span> extract_emails(</span><span class="vars">$str</span><span>){ </span></span></li>
<li><span>    <span class="comment">// This regular expression extracts all emails from a string:</span><span> </span></span></li>
<li class="alt"><span>    <span class="vars">$regexp</span><span> = </span><span class="string">'/([a-z0-9_\.\-])+\@(([a-z0-9\-])+\.)+([a-z0-9]{2,4})+/i'</span><span>; </span></span></li>
<li><span>    preg_match_all(<span class="vars">$regexp</span><span>, </span><span class="vars">$str</span><span>, </span><span class="vars">$m</span><span>); </span></span></li>
<li class="alt"><span> </span></li>
<li><span>    <span class="keyword">return</span><span> isset(</span><span class="vars">$m</span><span>[0]) ? </span><span class="vars">$m</span><span>[0] : </span><span class="keyword">array</span><span>(); </span></span></li>
<li class="alt"><span>} </span></li>
<li><span> </span></li>
<li class="alt"><span><span class="vars">$test_string</span><span> = 'This is a test string... </span></span></li>
<li><span> </span></li>
<li class="alt"><span>        test1@example.org </span></li>
<li><span> </span></li>
<li class="alt"><span>        Test different formats: </span></li>
<li><span>        test2@example.org; </span></li>
<li class="alt">
<span>        <a class="string">"test3@example.org"</a></span><span>>foobar </span>
</li>
<li><span>        <test4> </test4></span></li>
<li class="alt"><span> </span></li>
<li><span>        strange formats: </span></li>
<li class="alt"><span>        test5@example.org </span></li>
<li><span>        test6[at]example.org </span></li>
<li class="alt"><span>        test7@example.net.org.com </span></li>
<li><span>        test8@ example.org </span></li>
<li class="alt"><span>        test9@!foo!.org </span></li>
<li><span> </span></li>
<li class="alt"><span>        foobar </span></li>
<li><span>'; </span></li>
<li class="alt"><span> </span></li>
<li><span>print_r(extract_emails(<span class="vars">$test_string</span><span>)); </span></span></li>
</ol>
Copier après la connexion
Copier après la connexion

  [PHP]代码

<ol class="dp-c">
<li class="alt"><span><span class="keyword">function</span><span> extract_emails(</span><span class="vars">$str</span><span>){ </span></span></li>
<li><span>    <span class="comment">// This regular expression extracts all emails from a string:</span><span> </span></span></li>
<li class="alt"><span>    <span class="vars">$regexp</span><span> = </span><span class="string">'/([a-z0-9_\.\-])+\@(([a-z0-9\-])+\.)+([a-z0-9]{2,4})+/i'</span><span>; </span></span></li>
<li><span>    preg_match_all(<span class="vars">$regexp</span><span>, </span><span class="vars">$str</span><span>, </span><span class="vars">$m</span><span>); </span></span></li>
<li class="alt"><span> </span></li>
<li><span>    <span class="keyword">return</span><span> isset(</span><span class="vars">$m</span><span>[0]) ? </span><span class="vars">$m</span><span>[0] : </span><span class="keyword">array</span><span>(); </span></span></li>
<li class="alt"><span>} </span></li>
<li><span> </span></li>
<li class="alt"><span><span class="vars">$test_string</span><span> = 'This is a test string... </span></span></li>
<li><span> </span></li>
<li class="alt"><span>        test1@example.org </span></li>
<li><span> </span></li>
<li class="alt"><span>        Test different formats: </span></li>
<li><span>        test2@example.org; </span></li>
<li class="alt">
<span>        <a class="string">"test3@example.org"</a></span><span>>foobar </span>
</li>
<li><span>        <test4> </test4></span></li>
<li class="alt"><span> </span></li>
<li><span>        strange formats: </span></li>
<li class="alt"><span>        test5@example.org </span></li>
<li><span>        test6[at]example.org </span></li>
<li class="alt"><span>        test7@example.net.org.com </span></li>
<li><span>        test8@ example.org </span></li>
<li class="alt"><span>        test9@!foo!.org </span></li>
<li><span> </span></li>
<li class="alt"><span>        foobar </span></li>
<li><span>'; </span></li>
<li class="alt"><span> </span></li>
<li><span>print_r(extract_emails(<span class="vars">$test_string</span><span>)); </span></span></li>
</ol>
Copier après la connexion
Copier après la connexion



É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
Recommandations populaires
Tutoriels populaires
Plus>
Derniers téléchargements
Plus>
effets Web
Code source du site Web
Matériel du site Web
Modèle frontal