移除 HTML 标签
$text = strip_tags($input, "");
上面的函数主要是使用了
strip_tags,具体的使用说明参考。
返回 $start 和 $end 之间的文本
function GetBetween($content,$start,$end){
$r = explode($start, $content);
if (isset($r[1])){
$r = explode($end, $r[1]);
return $r[0];
}
return '';
}
将url转换成链接
$url = "Jean-Baptiste Jung (http://www.jb51.net)";
$url = preg_replace("#http://([A-z0-9./-] )#", '
$0', $url);
切分字符串为140个字符
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);
}
从字符串中提取email地址
function extract_emails($str){
// 此正则表达式从字符串中提取所有电子邮件:
$regexp = '/([a-z0-9_.-]) @(( [a-z0-9-]) .) ([a-z0-9]{2,4}) /i';
preg_match_all($regexp, $str, $m);
返回 isset($m[0]) ? $m[0] : 数组();
}
$test_string = '这是一个测试字符串...
test1@example.org
测试不同的格式:
test2@example.org组织;
foobar
奇怪的格式:
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));