资源描述:
《PHP常用字符串处理代码片段》由会员上传分享,免费在线阅读,更多相关内容在教育资源-天天文库。
1、移除HTML标签1.$text = strip_tags($input, ""); 返回$start和$end之间的文本1.function GetBetween($content,$start,$end){ 2. $r = explode($start, $content); 3. if (isset($r[1])){ 4. $r = explode($end, $r[1]); 5. return $r[0]; 6. } 7. return ''; 8.} 将url转换成链接1.$url =
2、 "Jean-Baptiste Jung (http://www.webdevcat.com)"; 2.$url = preg_replace("#http://([A-z0-9./-]+)#", '3、ation: none; font-weight: bold">$0
', $url); 切分字符串为140个字符1.function split_to_chunks($to,$text){ 2. $total_length = (140 - strlen($to)); 3. $text_arr = explode(" ",$text); 4. $i=0; 5. $message[0]=""; 6. foreach ($text_arr as $word){ 7. if ( strlen($mess
4、age[$i] . $word . ' ') <= $total_length ){ 8. if ($text_arr[count($text_arr)-1] == $word){ 9. $message[$i] .= $word; 10. } else { 11. $message[$i] .= $word . ' '; 12. } 13. } else { 14. $i++; 15.
5、 if ($text_arr[count($text_arr)-1] == $word){ 16. $message[$i] = $word; 17. } else { 18. $message[$i] = $word . ' '; 19. } 1. } 2. } 3. return $message; 4.} 删除字符串中的URL1.$string = preg_replace('/b(https?
6、
7、ftp
8、file)://[-A-Z0-9+&@#/%?=~_
9、$!:,.;]*[A-Z0-9+&@#/%=~_
10、$]/i', '', $string); 将字符串转成SEO友好的字符串1.function slug($str){ 2. $str = strtolower(trim($str)); 3. $str = preg_replace('/[^a-z0-9-]/', '-', $str); 4. $str = preg_replace('/-+/', "-", $str); 5. return $str; 6.
11、} 解析CSV文件1.$fh = fopen("contacts.csv", "r"); 2.while($line = fgetcsv($fh, 1000, ",")) { 3. echo "Contact: {$line[1]}"; 4.} 字符串搜索1.function contains($str, $content, $ignorecase=true){ 2. if ($ignorecase){ 3. $str = strtolower($str); 4. $content = strtolowe
12、r($content); 5. } 6. return strpos($conte