[英]print content of page by using htmlentities dont work for google.com
我使用此代码打印网页内容(源代码):
<?php
$url='http://cloob.com';
$ch=curl_init();
$timeout=5;
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
curl_setopt( $ch, CURLOPT_FOLLOWLOCATION, true );
// Get URL content
$lines_string=curl_exec($ch);
// close handle to release resources
curl_close($ch);
var_dump( htmlspecialchars($lines_string));
//echo htmlentities($lines_string);
//var_dump( $lines_string);
?>
这是有效的,但是当我将 URL 更改为https://google.com
不起作用,为什么?
当我在两种情况下直接使用(当我不使用htmlentities()
)它都有效......(我使用http://phpfiddle.org/ )
首先要做的是阅读文档:
如果输入字符串在给定的编码中包含无效的代码单元序列,则将返回一个空字符串,除非设置了 ENT_IGNORE 或 ENT_SUBSTITUTE 标志。
因此,我们可能在 PHP 解析 HTML 时遇到问题,或者编码不正确,或者 HTML 不正确。
当您为 google 网站使用正确的编码时,您会得到您想要的结果:
var_dump( htmlspecialchars($lines_string, ENT_COMPAT, 'ISO-8859-1'));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.