【问题标题】:simple html dom check if file is found简单的 html dom 检查是否找到文件
【发布时间】:2013-10-10 16:50:18
【问题描述】:

我已经工作了好几个小时,正在研究 simple_html_dom 的错误处理,它是用于 vine 过滤器网站的。我需要这样做,以便如果$videoid 没有字符串内容,它将要么 A. 结束 simple_html_dom 并输出一个普通字符串,例如“这里没有内容”之类的东西。我还是新手,请原谅我的错误..

我的错误结果: http://i.imgur.com/IyC5mJu.png

我的代码

<?php
include('include/simple_html_dom.php');
$website = 'http://vine.co/v/';
// if this is left empty site will display php errors saying page not found.
$videoid = 'haiKqBFA9Yw';
$html = file_get_html($website . $videoid);

if ($html->find('meta[property=og:image]')) {
  foreach ($html->find('meta[property=og:image]') as $element) {
    $img[] = $element->attr['content'];
    }
}

//Change http to https and remove versionId=    
$element->content = str_replace( 'https://', 'http://', $element->content );
$element->content = str_replace( 'versionId=', '', $element->content );

//Remove extra version id junk
$img = $element->content;
$element->content = substr($img, 0, strpos($img, "?"));


// My attempt using isset to determine if it has no content or if it does.. it did not work.
if (isset($videoid)) {
    echo $element->content;
}else{
    echo "It appears $videoid is empty.";
}
?>

把我的头发拉到这个上,我有一种强烈的感觉,这很容易解决。有人可以帮帮我吗?

尝试了其他变通方法: 如果字符串是 website 的数量而不是 kill dom

【问题讨论】:

  • 使用 simple_html_dom 版本 1.5 ($Rev: 208 $),您的代码适用于我并显示 JPG 图像的 URL。
  • 是的,代码工作得很好。但是我需要检查文件是否存在,所以我不会收到上述错误。

标签: php if-statement simpledom


【解决方案1】:

您可能想检查是否在脚本开头定义了$videoid。下方@file_get_html 中的& 符号将在找不到页面的情况下抑制错误消息。

<?php
include('include/simple_html_dom.php');
$website = 'http://vine.co/v/';
#$videoid = 'haiKqBFA9Yw';
$videoid = array_key_exists("videoid", $_GET)? $_GET["videoid"]: "";
if ($videoid != "") {
    $page = $website . $videoid;
    $html = @file_get_html($page);
    if ($html) {
        foreach ($html->find('meta[property=og:image]') as $element) {
            $image = $element->content;

            //Change http to https and remove versionId=
            $image = str_replace( 'https://', 'http://', $image );
            $image = str_replace( 'versionId=', '', $image );

            //Remove extra version id junk
            $image = substr($image, 0, strpos($image, "?"));

            $images[] = $image;
        }
        echo "<pre>" . print_r($images, TRUE) . "</pre>";
    } else {
        echo "Page not found: $page";
    }
} else {
    echo "It appears videoid is empty.";
}
?>

【讨论】:

  • 感谢八月的回复。但是,如果 It appears videoid is empty. 我确实修复了 #$videoid,则插入 videoId 后它会继续返回设置错误
  • @ErikJamesSmith 抱歉,但我不明白您所说的“看来 videoid 是空的”是什么意思。如果$videoid 是“haiKqBFA9Yw”,则它不为空。
  • 好吧,我很感谢 August 的帮助。但解决方案是这样的,if ($html === false) {
猜你喜欢
  • 1970-01-01
  • 2018-02-03
  • 1970-01-01
  • 2020-03-02
  • 1970-01-01
  • 1970-01-01
  • 2019-08-06
  • 1970-01-01
  • 2013-07-30
相关资源
最近更新 更多