首页 > 解决方案 > 是否可以调试此 PHP

问题描述

我真的抓住了这里的稻草。我不编写代码,我的 PHP 电子邮件扩展在编辑模板时工作得很好,除了这些错误。我的供应商不知道在哪个时区,而且反应迟钝。除了显而易见的——改变扩展、改变供应商等等,你们中的任何一个 PHP 人都可以看看这个代码块并告诉我哪里出了问题。我不知道如果断章取义,它是否有意义。错误是 PHP 注意:尝试从第 1344 行开始获取非对象的属性。这是从 1344 到 1370 的代码块。当 HTML 不是一个选项时,它显然与切换到纯文本有关。再次,在这里抓住稻草,但对于知识渊博的人来说,stackoverflow 是相当可靠的。

我正在重新粘贴代码以包含更完整的块。我还在行中添加了注释以指示四个警告出现的位置。它们看起来像这样://NEXT LINE IS ERROR ONE - LINE 1344 注意:试图获取非对象的属性

谢谢

<?php
    public static function isHTML($str) {
    $html = array('A','ABBR','ACRONYM','ADDRESS','APPLET','AREA','B','BASE','BASEFONT','BDO','BIG','BLOCKQUOTE',
        'BODY','BR','BUTTON','CAPTION','CENTER','CITE','CODE','COL','COLGROUP','DD','DEL','DFN','DIR','DIV','DL',
        'DT','EM','FIELDSET','FONT','FORM','FRAME','FRAMESET','H1','H2','H3','H4','H5','H6','HEAD','HR','HTML',
        'I','IFRAME','IMG','INPUT','INS','ISINDEX','KBD','LABEL','LEGEND','LI','LINK','MAP','MENU','META',
        'NOFRAMES','NOSCRIPT','OBJECT','OL','OPTGROUP','OPTION','P','PARAM','PRE','Q','S','SAMP','SCRIPT',
        'SELECT','SMALL','SPAN','STRIKE','STRONG','STYLE','SUB','SUP','TABLE','TBODY','TD','TEXTAREA','TFOOT',
        'TH','THEAD','TITLE','TR','TT','U','UL','VAR');

    return preg_match("~(&lt;\/?)\b(".implode('|', $html).")\b([^>]*&gt;)~i", $str, $c);
}

private function _html_to_plain_text($node) {
    if ($node instanceof DOMText) {
        return preg_replace("/\\s+/im", " ", $node->wholeText);
    }
    if ($node instanceof DOMDocumentType) {
        // ignore
        return "";
    }

    // Next
//NEXT LINE IS ERROR ONE - LINE 1344  Notice: Trying to get property of non-object
    $nextNode = $node->nextSibling;
    while ($nextNode != null) {
        if ($nextNode instanceof DOMElement) {
            break;
        }
        $nextNode = $nextNode->nextSibling;
    }
    $nextName = null;
    if ($nextNode instanceof DOMElement && $nextNode != null) {
        $nextName = strtolower($nextNode->nodeName);
    }

    // Previous
//NEXT LINE IS ERROR TWO - LINE 1357  Notice: Trying to get property of non-object
    $nextNode = $node->previousSibling;
    while ($nextNode != null) {
        if ($nextNode instanceof DOMElement) {
            break;
        }
        $nextNode = $nextNode->previousSibling;
    }
    $prevName = null;
    if ($nextNode instanceof DOMElement && $nextNode != null) {
        $prevName = strtolower($nextNode->nodeName);
    }
//NEXT LINE IS ERROR THREE - LINE 1369  Notice: Trying to get property of non-object
    $name = strtolower($node->nodeName);

    // start whitespace
    switch ($name) {
        case "hr":
            return "------\n";

        case "style":
        case "head":
        case "title":
        case "meta":
        case "script":
            // ignore these tags
            return "";

        case "h1":
        case "h2":
        case "h3":
        case "h4":
        case "h5":
        case "h6":
            // add two newlines
            $output = "\n";
            break;

        case "p":
        case "div":
            // add one line
            $output = "\n";
            break;

        default:
            // print out contents of unknown tags
            $output = "";
            break;
    }

    // debug $output .= "[$name,$nextName]";
//NEXT LINE IS ERROR FOUR - LINE 1408  Notice: Trying to get property of non-object
    if($node->childNodes){
        for ($i = 0; $i < $node->childNodes->length; $i++) {
            $n = $node->childNodes->item($i);

            $text = $this->_html_to_plain_text($n);

            $output .= $text;
        }
    }

    // end whitespace
    switch ($name) {
        case "style":
        case "head":
        case "title":
        case "meta":
        case "script":
            // ignore these tags
            return "";

        case "h1":
        case "h2":
        case "h3":
        case "h4":
        case "h5":
        case "h6":
            $output .= "\n";
            break;

        case "p":
        case "br":
            // add one line
            if ($nextName != "div")
                $output .= "\n";
            break;

        case "div":
        // add one line only if the next child isn't a div
            if (($nextName != "div" && $nextName != null) || ($node->hasAttribute('class') && strstr($node->getAttribute('class'), 'emailtemplateSpacing')))
                $output .= "\n";
            break;

        case "a":
            // links are returned in [text](link) format
            $href = $node->getAttribute("href");
            if ($href == null) {
                // it doesn't link anywhere
                if ($node->getAttribute("name") != null) {
                    $output = "$output";
                }
            } else {
                if ($href == $output || ($node->hasAttribute('class') && strstr($node->getAttribute('class'), 'emailtemplateNoDisplay'))) {
                    // link to the same address: just use link
                    $output;
                } else {
                    // No display
                    $output = $href . "\n" . $output;
                }
            }

            // does the next node require additional whitespace?
            switch ($nextName) {
                case "h1": case "h2": case "h3": case "h4": case "h5": case "h6":
                    $output .= "\n";
                    break;
            }

        default:
            // do nothing
    }

    return $output;
    }
}
?>

标签: phpemailixmldomelement

解决方案


听起来$node进入您的函数的private function _html_to_plain_text($node)变量不是正确的变量类型。可能是int, string, array, or null, 而不是它应该是的任何对象/类。您可能需要查看调用该函数的代码,而不是函数本身。

在 PHP 中进行调试本身就是一项技能。

  • 如果手动编码,请考虑var_export($node);在您怀疑存在问题的区域之前和之后插入语句,以便您可以检查它。
  • 如果您可以访问调试器(例如,Eclipse + XAMPP + XDebug),请使用它并逐行检查,通过将鼠标悬停在 $node 变量上或查看变量列表来检查它的内容。

推荐阅读