PHP- HTML解析::如何通过简单的html dom解析器获取网页的字符集值? [英] PHP- HTML parsing :: How can be taken charset value of webpage with simple html dom parser?

查看:93
本文介绍了PHP- HTML解析::如何通过简单的html dom解析器获取网页的字符集值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

PHP ::如何使用简单的 html dom解析器(utf-8 ,Windows-255等.)

PHP:: How can be taken charset value of webpage with simple html dom parser (utf-8, windows-255, etc..)?

备注:必须使用html dom解析器完成 http://simplehtmldom.sourceforge.net

remark: its have to be done with html dom parser http://simplehtmldom.sourceforge.net

Example1网页字符集输入:

<meta content="text/html; charset=utf-8" http-equiv="Content-Type">

结果:utf-8

Example2网页字符集输入:

<meta content="text/html; charset=windows-255" http-equiv="Content-Type">

结果:windows-255

我尝试了此操作(但不起作用):

$html = file_get_html('http://www.google.com/');
$el=$html->find('meta[content]',0);
echo $el->charset; 

应该改变什么? (我知道$ el->字符集不起作用)

What should be change? (I know that $el->charset not working)

谢谢

推荐答案

您必须使用正则表达式匹配字符串(希望您使用PCRE ...).

You'll have to match the string using a regular expression (I hope you have PCRE...).

$el=$html->find('meta[http-equiv=Content-Type]',0)
$fullvalue = $el->content;
preg_match('/charset=(.+)/', $fullvalue, $matches);
echo $matches[1];

不是很健壮,但应该可以.

Not very robust, but should work.

这篇关于PHP- HTML解析::如何通过简单的html dom解析器获取网页的字符集值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆