我使用简单的HTML dom解析器。 一切都适用于我的代码,但jqueryui不工作,对于一些网站,它不显示图像。 请查看实际网站here 请尝试使用简单的HTML dom parser。
请在带有网址的文本字段中输入网址。 您可以看到未加载图像且滑块无法正常工作。 这是我的代码
<?php
include_once 'simple_html_dom.php';
$data = new simple_html_dom();
if ( isset($_REQUEST['url_name']) ) {
if ( strpos($_REQUEST['url_name'], "http://") === false && strpos($_REQUEST['url_name'], "//") === false ) {
$_REQUEST['url_name']="http://".$_REQUEST['url_name'];
}
$url_name = $_REQUEST['url_name'];
if ( strpos($_REQUEST['url_name'], "/") === false ) {
$url_name = $_REQUEST['url_name'].'/';
}
// Load HTML from an URL
$data->load_file($_REQUEST['url_name']);
foreach ( $data->find('img') as $element ) {
$element->target='_blank';
if ( strpos($element, ".com") === false
&& strpos($element, ".net") === false
&& strpos($element, ".org") === false
&& strpos($element, "http://") === false
&& strpos($element, "https://") === false
){
$element->src=$url_name.$element->src;
}
}
foreach ( $data->find('style') as $element ) {
if ( strpos($element, ".com") === false
&& strpos($element, ".net") === false
&& strpos($element, ".org") === false
&& strpos($element, "http://") === false
&& strpos($element, "https://") === false
){
$element->src=$url_name.$element->src;
}
}
foreach ( $data->find('script') as $element ) {
if ( strpos($element, ".com") === false
&& strpos($element, ".net") === false
&& strpos($element, ".org") === false
&& strpos($element, "http://") === false
&& strpos($element, "https://") === false
){
$element->src=$url_name.$element->src;
}
}
foreach ( $data->find('link') as $element ) {
if ( strpos($element, ".com") === false
&& strpos($element, ".net") === false
&& strpos($element, ".org") === false
&& strpos($element, "http://") === false
&& strpos($element, "https://") === false
){
$element->href=$url_name.$element->href;
}
}
foreach ( $data->find('a') as $element ) {
if ( strpos($element->href, ".com") === false
&& strpos($element->href, ".net") === false
&& strpos($element->href, ".org") === false
&& strpos($element->href, "http://") === false
&& strpos($element->href, "https://") === false
){
$element->href = "form_submit.php?url_name=".$url_name.$element->href;
} else {
$element->href = "form_submit.php?url_name=".$element->href;
}
}
echo $newHtml;
}
?>
答案 0 :(得分:0)
$newHtml
从未在您的代码中的任何位置定义过。simple_html_dom
课程是什么?我假设它是这样的:http://simplehtmldom.sourceforge.net/
echo $data;
。由于我并不真正了解你的目标,simple_html_dom
实际上是什么,我能做的就是向你介绍一些更高级的东西
编码概念。
调整后的代码:
<?php
include_once 'simple_html_dom.php';
$data = new simple_html_dom();
if ( isset($_REQUEST['url_name']) ) {
if ( strpos($_REQUEST['url_name'], "http://") === false && strpos($_REQUEST['url_name'], "//") === false ) {
$_REQUEST['url_name']="http://".$_REQUEST['url_name'];
}
$data->my_url_name = $_REQUEST['url_name'];
if ( strpos($_REQUEST['url_name'], "/") === false ) {
$data->my_url_name = $_REQUEST['url_name'].'/';
}
echo "<div style='border:1px solid blue;'>";
// Load HTML from an URL
$data->load_file($_REQUEST['url_name']);
modifyUrls($data, 'img', array('target' => '_blank'));
modifyUrls($data, 'style');
modifyUrls($data, 'script');
modifyUrls($data, 'link', array(), 'href');
foreach ( $data->find('a') as $element ) {
if ( checkurl($element->href) ) {
echo $element->href = "form_submit.php?url_name=". $data->my_url_name . $element->href;
} else {
echo $element->href = "form_submit.php?url_name=". $element->href;
}
echo "<br>";
}
echo "</div><div style='border:1px solid red;'>";
echo $data;
echo "</div>";
}
function modifyUrls(&$dataObj, $target, $options = array(), $urlAttribute = "src") {
foreach ( $dataObj->find($target) as $element ) {
if ( !empty($options) ) {
foreach ($options as $field => $value) {
$element->{$field} = $value;
}
}
if ( checkurl($element) ) {
echo $element->{$urlAttribute} = $dataObj->my_url_name . $element->{$urlAttribute};
echo "<br>";
}
}
}
function checkurl($url){
if ( strpos($url, ".com") === false
&& strpos($url, ".net") === false
&& strpos($url, ".org") === false
&& strpos($url, "http://") === false
&& strpos($url, "https://") === false
){
return true;
}
return false;
}