php远程获取网站的关键字信息(并附:php判断字符串编码)
很明显,php100是gbk编码的网站。现在获取它的关键字,并无乱码的显示出来,然后好保存到数据库。
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<?phpfunction check_utf8($checkString){
if (preg_match("/^([".chr(228)."-".chr(233)."]{1}[".chr(128)."-".chr(191)."]{1}[".chr(128)."-".chr(191)."]{1}){1}/",$checkString) == true || preg_match("/([".chr(228)."-".chr(233)."]{1}[".chr(128)."-".chr(191)."]{1}[".chr(128)."-".chr(191)."]{1}){1}$/",$checkString) == true || preg_match("/([".chr(228)."-".chr(233)."]{1}[".chr(128)."-".chr(191)."]{1}[".chr(128)."-".chr(191)."]{1}){2,}/",$checkString) == true){
return true;
}else{
return false;
}}$meta = get_meta_tags('http://www.php100.com');
echo '源数据:'.$keywords = $meta['keywords'];
echo '<pre /><pre />';
if(check_utf8($keywords) == 0){
$keywords = iconv("GBK","UTF-8",$keywords);
}$keywords = explode(',', $keywords );
$keywords = array_map( 'trim', $keywords );// 函数返回用户自定义函数作用后的数组【去除空格】
$keywords = array_filter( $keywords );//如果没有提供 callback 函数,array_filter() 将删除 input 中所有等值为 FALSE 的条目。
print_r( $keywords );
?> |
本文转自许琴 51CTO博客,原文链接:http://blog.51cto.com/xuqin/1349694,如需转载请自行联系原作者