PHP 常用字符串处理代码片段
                                                            生活随笔
收集整理的這篇文章主要介紹了
                                PHP 常用字符串处理代码片段
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.                        
                                移除 HTML 標簽
- $text?=?strip_tags($input,?"");?
- function?GetBetween($content,$start,$end){?
- ????$r?=?explode($start,?$content);?
- ????if?(isset($r[1])){?
- ????????$r?=?explode($end,?$r[1]);?
- ????????return?$r[0];?
- ????}?
- ????return?'';?
- }?
- $url?=?"Jean-Baptiste?Jung?(http://www.webdevcat.com)";?
- $url?=?preg_replace("#http://([A-z0-9./-]+)#",?'<a?href="http://www.catswhocode.com/blog/$1"?>$0</a>',?$url);?
- function?split_to_chunks($to,$text){?
- ????$total_length?=?(140?-?strlen($to));?
- ????$text_arr?=?explode("?",$text);?
- ????$i=0;?
- ????$message[0]="";?
- ????foreach?($text_arr?as?$word){?
- ????????if?(?strlen($message[$i]?.?$word?.?'?')?<=?$total_length?){?
- ????????????if?($text_arr[count($text_arr)-1]?==?$word){?
- ????????????????$message[$i]?.=?$word;?
- ????????????}?else?{?
- ????????????????$message[$i]?.=?$word?.?'?';?
- ????????????}?
- ????????}?else?{?
- ????????????$i++;?
- ????????????if?($text_arr[count($text_arr)-1]?==?$word){?
- ????????????????$message[$i]?=?$word;?
- ????????????}?else?{?
- ????????????????$message[$i]?=?$word?.?'?';?
- ????????????}?
- ????????}?
- ????}?
- ????return?$message;?
- }?
刪除字符串中的URL
- $string?=?preg_replace('/\b(https?|ftp|file):\/\/[-A-Z0-9+&@#\/%?=~_|$!:,.;]*[A-Z0-9+&@#\/%=~_|$]/i',?'',?$string);?
?
將字符串轉成SEO友好的字符串
- function?slug($str){?
- ????$str?=?strtolower(trim($str));?
- ????$str?=?preg_replace('/[^a-z0-9-]/',?'-',?$str);?
- ????$str?=?preg_replace('/-+/',?"-",?$str);?
- ????return?$str;?
- }?
?
?
解析 CSV 文件
- $fh?=?fopen("contacts.csv",?"r");?
- while($line?=?fgetcsv($fh,?1000,?","))?{?
- ????echo?"Contact:?{$line[1]}";?
- }?
- function?contains($str,?$content,?$ignorecase=true){?
- ????if?($ignorecase){?
- ????????$str?=?strtolower($str);?
- ????????$content?=?strtolower($content);?
- ????}?
- ????return?strpos($content,$str)???true?:?false;?
- }?
- function?String_Begins_With($needle,?$haystack?{?
- ????return?(substr($haystack,?0,?strlen($needle))==$needle);?
- }?
- function?extract_emails($str){?
- ????//?This?regular?expression?extracts?all?emails?from?a?string:?
- ????$regexp?=?'/([a-z0-9_\.\-])+\@(([a-z0-9\-])+\.)+([a-z0-9]{2,4})+/i';?
- ????preg_match_all($regexp,?$str,?$m);?
- ?
- ????return?isset($m[0])???$m[0]?:?array();?
- }?
- ?
- $test_string?=?'This?is?a?test?string...?
- ?
- ????????test1@example.org?
- ?
- ????????Test?different?formats:?
- ????????test2@example.org;?
- ????????<a?href="test3@example.org">foobar</a>?
- ????????<test4@example.org>?
- ?
- ????????strange?formats:?
- ????????test5@example.org?
- ????????test6[at]example.org?
- ????????test7@example.net.org.com?
- ????????test8@?example.org?
- ????????test9@!foo!.org?
- ?
- ????????foobar?
- ';?
- ?
- print_r(extract_emails($test_string));?
- function?extract_emails($str){?
- ????//?This?regular?expression?extracts?all?emails?from?a?string:?
- ????$regexp?=?'/([a-z0-9_\.\-])+\@(([a-z0-9\-])+\.)+([a-z0-9]{2,4})+/i';?
- ????preg_match_all($regexp,?$str,?$m);?
- ?
- ????return?isset($m[0])???$m[0]?:?array();?
- }?
- ?
- $test_string?=?'This?is?a?test?string...?
- ?
- ????????test1@example.org?
- ?
- ????????Test?different?formats:?
- ????????test2@example.org;?
- ????????<a?href="test3@example.org">foobar</a>?
- ????????<test4@example.org>?
- ?
- ????????strange?formats:?
- ????????test5@example.org?
- ????????test6[at]example.org?
- ????????test7@example.net.org.com?
- ????????test8@?example.org?
- ????????test9@!foo!.org?
- ?
- ????????foobar?
- ';?
- ?
- print_r(extract_emails($test_string));?
轉載于:https://www.cnblogs.com/rmbteam/archive/2011/11/06/2238016.html
總結
以上是生活随笔為你收集整理的PHP 常用字符串处理代码片段的全部內容,希望文章能夠幫你解決所遇到的問題。
 
                            
                        - 上一篇: 男生会把什么样的女生当做好哥们?
- 下一篇: 一个虚函数和虚继承的问题。
