移除
HTML 标签
1. $text = strip_tags($input, "");
返回 $start 和 $end 之间的文本 1. function GetBetween($content,$start,$end){ 2. 3. 4. 5. 6. 7. 8. } } return ''; $r = explode($start, $content); if (isset($r[1])){ $r = explode($end, $r[1]); return $r[0];
将 url 转换成链接 1. $url = "Jean-Baptiste Jung (
webdevcat)"; 2. $url = preg_replace("#([A-z0-9./-]+)#", '
$0', $url);
切分字符串为 140 个字符 1. function split_to_chunks($to,$text){ 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. 13. 14. 15. 16. 17. 18. 19. } } } else { $i++; if ($text_arr[count($text_arr)-1] == $word){ $message[$i] = $word; } else { $message[$i] = $word . ' '; $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 . ' ';
20. 21. 22. 23. } }
}
return $message;
删除字符串中的 URL 1. $string = preg_replace('/\b(https?|ftp|file):\/\/[-A-Z0-9+&;@#\/%?=~_| $!:,.;]*[A-Z0-9+&;@#\/%=~_|$]/i', '', $string);
将字符串转成 SEO 友好的字符串
1. function slug($str){ 2. 3. 4. 5. 6. } $str = strtolower(trim($str)); $str = preg_replace('/[^a-z0-9-]/', '-', $str); $str = preg_replace('/-+/', "-", $str); return $str;
解析 CSV 文件
1. $fh = fopen("contacts.csv", "r"); 2. while($line = fgetcsv($fh, 1000, ",")) { 3. 4. } echo "Contact: {$line[1]}";
字符串
搜索 1. function contains($str, $content, $ignorecase=true){ 2. 3. 4. 5. 6. 7. } } return strpos($content,$str) ? true : false; if ($ignorecase){ $str = strtolower($str); $content = strtolower($content);
检查字符串是否以某个串开始 1. function String_Begins_With($needle, $haystack { 2. 3. } return (substr($haystack, 0, strlen($needle))==$needle);
从字符串中提取 email 地址
1. function extract_emails($str){ 2. 3. '; 4. 5. 6. 7. } 8. 9. $test_string = 'This is a test string... 10. 11. 12. 13. 14. 15. 16. 17. 18. 19. 20. 21. 22. 23. 24. 25. 26. '; 27. 28. print_r(extract_emails($test_string)); foobar strange formats: test5@example.org test6[at]example.org test7@example.net.org test8@ example.org test9@!foo!.org Test different formats: test2@example.org;
foobar test1@example.org return isset($m[0]) ? $m[0] : array(); preg_match_all($regexp, $str, $m); // This regular expression extracts all emails from a string: $regexp = '/([a-z0-9_\.\-])+\@(([a-z0-9\-])+\.)+([a-z0-9]{2,4})+/i
[PHP]代码 代码 1. function extract_emails($str){ 2. 3. '; 4. 5. 6. 7. } 8. 9. $test_string = 'This is a test string... 10. 11. test1@example.org return isset($m[0]) ? $m[0] : array(); preg_ma
tch_all($regexp, $str, $m); // This regular expression extracts all emails from a string: $regexp = '/([a-z0-9_\.\-])+\@(([a-z0-9\-])+\.)+([a-z0-9]{2,4})+/i
12. 13. 14. 15. 16. 17. 18. 19. 20. 21. 22. 23. 24. 25. 26. '; 27. 28. print_r(extract_emails($test_string)); foobar strange formats: test5@example.org test6[at]example.org test7@example.net.org test8@ example.org test9@!foo!.org Test different formats: test2@example.org; foobar