【问题标题】:Creating default object from empty value warning is generated when the tag is not present. How to resolve?当标签不存在时,会生成从空值创建默认对象警告。如何解决?
【发布时间】:2013-11-12 09:32:33
【问题描述】:

严格标准:从 行

中的空值创建默认对象
include('functions/simple_html_dom.php');
$content=str_get_html($submission);
$content->find('a', 0)->class='article-link';
$content->find('a', 0)->target='_blank';
$content->find('a', 0)->rel='nofollow';
$content->find('img', 0)->class='article-inner-image';

变量$submission 将从表单中获取 POST 数据。它可能包含也可能不包含<a><img> 标签。当两者都存在时,不会引发警告。但是当一个或两个标签都不存在时,它会抛出这个警告。我该如何解决这个问题?

【问题讨论】:

标签: php simple-html-dom


【解决方案1】:

在解析 HTML 时,在更改某些元素之前检查它们是否存在。

// Check if and '<a>' tag was submitted
if($a = $content->find('a', 0)) {

  // If so, set attributes on it.
  $a->class='article-link';
  ...
}

// Check if an '<img>' tag was submitted
if($img = $content->find('img', 0)) {
  ...
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-03-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-06-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多