【发布时间】:2012-09-14 15:50:18
【问题描述】:
我想做一件简单的事情:从字符串(即 HTML 文件)中提取代码的某些特定部分。
例如:
//Get a string from a website:
$homepage = file_get_contents('http://mywebsite.org');
//Then, search a particulare substring between two strings:
echo magic_substr($homepage, "<script language", "</script>");
//where magic_substr is this function (find in this awesome website):
function magic_substr($haystack, $start, $end) {
$index_start = strpos($haystack, $start);
$index_start = ($index_start === false) ? 0 : $index_start + strlen($start);
$index_end = strpos($haystack, $end, $index_start);
$length = ($index_end === false) ? strlen($end) : $index_end - $index_start;
return substr($haystack, $index_start, $length);
}
在这种情况下,我想要获得的输出是页面上的所有脚本。但是就我而言,我只能获得第一个脚本。我认为这是正确的,因为没有任何递归。但我不知道最好的方法是什么!有什么建议吗?
【问题讨论】:
-
只要你不使用DOM Parser 在 html 文档中查找内容,小狗就会死得很惨。
-
嗨,我尝试了 Simple Dom Parser,但遇到了“max_nested_level”问题......所以我以这种方式移动:)
-
max_nested_level 有什么问题?我相信 PHP Simple HTML Dom Parser 可以做到这一点。
-
但是,请注意,DOM Parser 仅在 HTML 有效时才起作用。
-
使用简单的 HTML Dom Parser 我达到了嵌套函数的限制,即 100,但我找不到如何更改这个值.. 我在这个网站上阅读了很多关于嵌套级别的内容,但我没有找到解决方案..所以我想以这种方式移动..我知道这有点难看:-D