为什么我正确定义的变量不能正确计算长度(并且随后在我的代码的其余部分中工作)?
我采用了 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;
}
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;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您之前的问题告诉您检查 href 的长度以查看其是否有内容。这是正确的,因为 hrefs 是一个数组。它有长度并且支持长度属性。
get_the_content()
返回一个字符串(请参阅文档)。要检查字符串长度,请使用 strlen
要检查是否为 null,请使用 is_null
要检查是否设置使用 isset
isset 和 is_null 之间的区别
更新
您问为什么您的代码在以下行分支错误:
您还说
$xpath< /代码>是在代码中进一步定义。但是,您重新定义了
$doc
那么为什么$xpath
中会有正确的值呢?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:
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?