【问题标题】:Why doesn't my properly defined variable evaluate for length correctly (and subsequently work in the rest of my code)?为什么我正确定义的变量不能正确评估长度(随后在我的其余代码中工作)?
【发布时间】:2011-11-19 01:51:17
【问题描述】:

我采用了 Marc B 建议的答案,但当我在 while 循环后回显它时,变量中仍然没有任何结果,因此我添加了一些检查以显示通过代码处理的事物的状态。当 if/else 语句接下来运行时,它显示变量有长度的结果。下一个 if/else 语句分支到 else 语句,然后在下一个 if/else 中使用 else 语句,表示 xpath 什么也没找到。所以很明显,当我去使用变量 $BEmp3s 时,它里面什么都没有。

这对我来说没有多大意义,因为一开始,$BEpost_content 的回声显示了完整的正确内容,但对其长度的评估显示什么都没有/NULL?请帮忙!

<?php
    // Start MP3 URL
    $doc   = new DOMDocument();
    $doc->strictErrorChecking = FALSE;

    $xpath = new DOMXpath($doc);
    // End MP3 URL

    $a = 1;
    if (have_posts()) :
        while ( have_posts() ) : the_post();
?>
<?php
$BEpost_content = get_the_content();
if (strlen($BEpost_content) > 0) {
    echo "<div id='debug_content'>get_the_content has something</div>";
} else {
    echo "<div id='debug_content'>BEpost_content is empty</div>" ;
};
$success = $doc->loadHTML($BEpost_content);
if ($success === FALSE) {
    echo "<div id='debug_loadcontent'>loadHTML failed to load post content</div>";
} else {
    $hrefs = $xpath->query("//a[contains(@href,'mp3')]/@href");
    if ($hrefs->length > 0) {
        echo "<div id='debug_xpath'>xpath found something</div>";
    } else {
        echo "<div id='debug_xpath'>xpath found nothing</div>";
    };
    $BEmp3s = $hrefs->item(0);
};
?>

据我所知,这是 get_the_content() 函数,它返回一个字符串:

function get_the_content($more_link_text = null, $stripteaser = 0) {
global $post, $more, $page, $pages, $multipage, $preview;

if ( null === $more_link_text )
    $more_link_text = __( '(more...)' );

$output = '';
$hasTeaser = false;

// If post password required and it doesn't match the cookie.
if ( post_password_required($post) ) {
    $output = get_the_password_form();
    return $output;
}

if ( $page > count($pages) ) // if the requested page doesn't exist
    $page = count($pages); // give them the highest numbered page that DOES exist

$content = $pages[$page-1];
if ( preg_match('/<!--more(.*?)?-->/', $content, $matches) ) {
    $content = explode($matches[0], $content, 2);
    if ( !empty($matches[1]) && !empty($more_link_text) )
        $more_link_text = strip_tags(wp_kses_no_null(trim($matches[1])));

    $hasTeaser = true;
} else {
    $content = array($content);
}
if ( (false !== strpos($post->post_content, '<!--noteaser-->') && ((!$multipage) || ($page==1))) )
    $stripteaser = 1;
$teaser = $content[0];
if ( ($more) && ($stripteaser) && ($hasTeaser) )
    $teaser = '';
$output .= $teaser;
if ( count($content) > 1 ) {
    if ( $more ) {
        $output .= '<span id="more-' . $post->ID . '"></span>' . $content[1];
    } else {
        if ( ! empty($more_link_text) )
            $output .= apply_filters( 'the_content_more_link', ' <a href="' . get_permalink() . "#more-{$post->ID}\" class=\"more-link\">$more_link_text</a>", $more_link_text );
        $output = force_balance_tags($output);
    }

}
if ( $preview ) // preview fix for javascript bug with foreign languages
    $output =   preg_replace_callback('/\%u([0-9A-F]{4})/', '_convert_urlencoded_to_entities', $output);

return $output;

}

【问题讨论】:

  • $BEpost_content是什么对象?
  • 你能发布你的函数 get_the_content() 吗?
  • $BEpost_content 是一个字符串,get_the_content() 是 Wordpress 中提供的标准函数,这里有一个链接,它的源代码hitchhackerguide.com/2011/02/12/get_the_content

标签: php wordpress variables evaluate


【解决方案1】:

你之前的问题告诉你检查hrefs的长度,看它是否有内容。这是正确的,因为 hrefs 是一个数组。它有一个长度并支持长度属性。 get_the_content() 返回一个字符串(参见docs)。

要检查字符串长度,请使用strlen

要检查是否为空,请使用is_null

检查是否设置使用isset

Difference between isset and is_null

更新

您在以下行询问了为什么您的代码分支不正确:

$hrefs = $xpath->query("//a[contains(@href,'mp3')]/@href");

您还说$xpath 在代码中进一步定义。但是,您重新定义了$doc,那么为什么$xpath 中会有正确的值?

$success = $doc->loadHTML($BEpost_content);  //you change $doc here!
$xpath = new DOMXpath($doc);  //so perhaps you should load it into xpath here?
$hrefs = $xpath->query("//a[contains(@href,'mp3')]/@href"); //don't know what this query does. maybe it is broken.

【讨论】:

  • 您调用 $hrefs = $xpath-&gt;query,但从未定义 xpath。至少在您提供的代码中不是。您可能想在标题中特别提到 xpath 的新问题中提出这个问题。
  • 这里的所有代码都在一个while循环中。就在循环开始之前,xpath 实际上被定义为 $xpath = new DOMXpath($doc);
  • 您在修改 doc 时永远不会重新定义 xpath。请参阅我的更新答案。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-03-22
  • 2016-11-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-11-15
  • 1970-01-01
相关资源
最近更新 更多