Why doesn't my properly defined variable evaluate for length correctly (and subsequently work in the rest of my code)?

六眼飞鱼酱① 提交于 2020-01-06 14:17:09

问题


I employed the answer Marc B suggested and still got nothing in the variable when I echo'd it after the while loop, so I added a few checks to show status of things as they were processed through the code. When the if/else statement runs next, it shows the result that there is length to the variable. The next if/else statement branches to the else statement and then takes the else statement in the next if/else saying the xpath found nothing. So obviously when I go to use the variable $BEmp3s, it has nothing in it.

This doesn't make much sense to me since in the beginning, the echo of $BEpost_content shows the proper content in its entirety but the evaluation on its length shows nothing/NULL? Please help!

<?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);
};
?>

Here is the function get_the_content() which returns a string to my knowledge:

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;

}


回答1:


Your previous question told you to check the length of hrefs to see whether it had content. This is correct because hrefs is an array. It has a length and supports the length property. get_the_content() returns a string (see docs).

To check string length use strlen

To check if null use is_null

To check if set use isset

Difference between isset and is_null

Update

You asked why your code branches incorrectly at the following line:

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

You also say that $xpath is defined further up in the code. However, you redefine $doc so why would $xpath have the correct values in it?

$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.


来源:https://stackoverflow.com/questions/8191249/why-doesnt-my-properly-defined-variable-evaluate-for-length-correctly-and-subs

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!