[代码] 移除 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 = "Jean-Baptiste Jung (http://www.webdevcat.com)"; |
2 |
$url = preg_replace("#http://([A-z0-9./-]+)#", '<a href="http://www.catswhocode.com/blog/$1" style="font-size: 12px; vertical-align: baseline; background-color: transparent; margin: 0px; padding: 0px; color: #3777af; text-decoration: none; font-weight: bold">$0</a>', $url); |
[代码] 切分字符串为140个字符
01 |
function split_to_chunks($to,$text){ |
02 |
$total_length = (140 - strlen($to)); |
03 |
$text_arr = explode(" ",$text); |
04 |
$i=0; |
05 |
$message[0]=""; |
06 |
foreach ($text_arr as $word){ |
07 |
if ( strlen($message[$i] . $word . ' ') <= $total_length ){ |
08 |
if ($text_arr[count($text_arr)-1] == $word){ |
09 |
$message[$i] .= $word; |
10 |
} else { |
11 |
$message[$i] .= $word . ' '; |
12 |
} |
13 |
} else { |
14 |
$i++; |
15 |
if ($text_arr[count($text_arr)-1] == $word){ |
16 |
$message[$i] = $word; |
17 |
} else { |
18 |
$message[$i] = $word . ' '; |
19 |
} |
20 |
} |
21 |
} |
22 |
return $message; |
23 |
} |
[代码] 删除字符串中的URL
1 |
$string = preg_replace('/\b(https?|ftp|file):\/\/[-A-Z0-9+&@#\/%?=~_|$!:,.;]*[A-Z0-9+&@#\/%=~_|$]/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 |
} |
[代码] 解析 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 = strtolower($content); |
5 |
} |
6 |
return strpos($content,$str) ? true : false; |
7 |
} |
[代码] 检查字符串是否以某个串开始
1 |
function String_Begins_With($needle, $haystack { |
2 |
return (substr($haystack, 0, strlen($needle))==$needle); |
3 |
} |
[代码] 从字符串中提取email地址
01 |
function extract_emails($str){ |
02 |
// This regular expression extracts all emails from a string: |
03 |
$regexp = '/([a-z0-9_\.\-])+\@(([a-z0-9\-])+\.)+([a-z0-9]{2,4})+/i'; |
04 |
preg_match_all($regexp, $str, $m); |
05 |
06 |
return isset($m[0]) ? $m[0] : array(); |
07 |
} |
08 |
09 |
$test_string = 'This is a test string... |
10 |
11 |
test1@example.org |
12 |
13 |
Test different formats: |
14 |
test2@example.org; |
15 |
<a href="test3@example.org">foobar</a> |
16 |
<test4@example.org> |
17 |
18 |
strange formats: |
19 |
test5@example.org |
20 |
test6[at]example.org |
21 |
test7@example.net.org.com |
22 |
test8@ example.org |
23 |
test9@!foo!.org |
24 |
25 |
foobar |
26 |
'; |
27 |
28 |
print_r(extract_emails($test_string)); |
[代码] [PHP]代码
01 |
function extract_emails($str){ |
02 |
// This regular expression extracts all emails from a string: |
03 |
$regexp = '/([a-z0-9_\.\-])+\@(([a-z0-9\-])+\.)+([a-z0-9]{2,4})+/i'; |
04 |
preg_match_all($regexp, $str, $m); |
05 |
06 |
return isset($m[0]) ? $m[0] : array(); |
07 |
} |
08 |
09 |
$test_string = 'This is a test string... |
10 |
11 |
test1@example.org |
12 |
13 |
Test different formats: |
14 |
test2@example.org; |
15 |
<a href="test3@example.org">foobar</a> |
16 |
<test4@example.org> |
17 |
18 |
strange formats: |
19 |
test5@example.org |
20 |
test6[at]example.org |
21 |
test7@example.net.org.com |
22 |
test8@ example.org |
23 |
test9@!foo!.org |
24 |
25 |
foobar |
26 |
'; |
27 |
28 |
print_r(extract_emails($test_string)); |

浙公网安备 33010602011771号