Php html可以';不能编辑吗?
考虑一下这段简单的代码,它使用PHP简单HTML DOM解析器正常工作,它输出当前社区Php html可以';不能编辑吗?,php,parsing,simple-html-dom,Php,Parsing,Simple Html Dom,考虑一下这段简单的代码,它使用PHP简单HTML DOM解析器正常工作,它输出当前社区 <?php //PHP Simple HTML DOM Parser from simplehtmldom.sourceforge.net include_once('simple_html_dom.php'); //Target URL $url = 'http://stackoverflow.com/questions/ask'; //Getting c
<?php
//PHP Simple HTML DOM Parser from simplehtmldom.sourceforge.net
include_once('simple_html_dom.php');
//Target URL
$url = 'http://stackoverflow.com/questions/ask';
//Getting content of $url
$doo = file_get_html($url);
//Passing the variable $doo to $abd
$abd = $doo ;
//Trying to find the word "current community"
echo $abd->find('a', 0)->innertext; //Output: current community.
?>
为什么我不能编辑从文件\u get\u html获取的字符串?我需要编辑它有很多重要原因(比如在处理页面的html内容之前删除一些脚本)。我也不明白为什么它会给出一个错误,即无法找到文件_get_html()(很明显,我们正在从第一个代码导入正确的解析器)
附加说明:
我尝试过所有这些变化:
include_once('simple_html_dom.php');
require_once('simple_html_dom.php');
include('simple_html_dom.php');
require('simple_html_dom.php');
$doo
不是字符串!它是一个对象,一个简单HTMLDOM的实例。不能对字符串调用->
方法,只能对对象调用。不能将此对象视为字符串。试图将某些东西连接到它是没有意义的<代码中的code>$abd是对象与字符串连接的结果;这会导致字符串或错误,具体取决于对象的详细信息。它当然不会产生一个可用的对象,所以您当然不能执行$abd->find()
如果要修改页面内容,请使用对象提供的DOM API进行修改。file\u get\u html()
返回一个对象,而不是字符串。尝试将字符串连接到对象将调用该对象的\u toString()
方法(如果存在),并且该操作将返回一个字符串。字符串没有find()
方法
如果要按照上述操作,请先读取文件内容并连接额外的字符串:
$content = file_get_contents('someFile.html');
$content .= "someString";
$domObject = str_get_html($content);
或者,使用
file\u get\u html()
读取文件,并使用DOM API对其进行操作。应该有错误,但不是那个错误。这毫无意义,真的。代码1工作得很好。那么$abd是一个对象吗?严格来说,在代码1中,$abd
是对$doo
的引用的副本。$abd
和$doo
都指向同一个对象非常感谢您编写的代码。工作得很好。
include_once('simple_html_dom.php');
require_once('simple_html_dom.php');
include('simple_html_dom.php');
require('simple_html_dom.php');
$content = file_get_contents('someFile.html');
$content .= "someString";
$domObject = str_get_html($content);