php 基于整数的 if 语句不显示结果?
我正在使用以下 preg_match
:
preg_match( '!<div class="thumblock ">(.*)</div>!si' , wp_gdsr_render_article_thumbs(0, false, "", 0, "", false) , $n );
$thumbs_string = strip_tags( $n[1] );
提取跨度标记之间的数字:(
<div class="thumblock ">
<span class="rating-result">
<div id="gdsr_thumb_text_12_a" class="gdt-size-20 voted inactive gdthumbtext">+1</div>
</span>
<div class="ratingtext ">
<div class="raterclear"></div>
</div>
在上面的示例中,结果是一个字符串:“+1”)
所以我尝试用这个将其转换为整数:
$thumbs_number = (int)$thumbs_string;
在此函数中使用:
function get_rating_class($thumbs_number) {
if ($thumbs_number < 0) return ' bad';
if ($thumbs_number < 2) return ' average';
if ($thumbs_number < 4) return ' good';
return ' excellent';
}
function rating_class($thumbs_number) {
echo get_rating_class($thumbs_number);
}
输出 div 类:
<div class="topic-like-count<?php rating_class($thumbs_number); ?>">
我什至做了 var_dump()
:
<h2><?php var_dump($thumbs_string); ?></h2>
<h2><?php var_dump($thumbs_number); ?></h2>
结果是:
分别是 string(2) "+1"
和 int(1)
(我直接将它们复制/粘贴到这里)。
但没有输出 div 类。
有什么建议来解决这个问题吗?
编辑:
该类确实在 HTML 源中输出,但它没有任何效果(并且我的样式表没有被缓存)。我有该函数的另一个版本,它不会在 span 标签周围添加额外的 div,并且该版本可以工作,但不幸的是我需要该 div。
I'm using the following preg_match
:
preg_match( '!<div class="thumblock ">(.*)</div>!si' , wp_gdsr_render_article_thumbs(0, false, "", 0, "", false) , $n );
$thumbs_string = strip_tags( $n[1] );
To extract the number between the span tags:
<div class="thumblock ">
<span class="rating-result">
<div id="gdsr_thumb_text_12_a" class="gdt-size-20 voted inactive gdthumbtext">+1</div>
</span>
<div class="ratingtext ">
<div class="raterclear"></div>
</div>
(in the example above, result is a string: "+1")
So I tried converting it into an integer with this:
$thumbs_number = (int)$thumbs_string;
which is used in this function:
function get_rating_class($thumbs_number) {
if ($thumbs_number < 0) return ' bad';
if ($thumbs_number < 2) return ' average';
if ($thumbs_number < 4) return ' good';
return ' excellent';
}
function rating_class($thumbs_number) {
echo get_rating_class($thumbs_number);
}
to output a div class:
<div class="topic-like-count<?php rating_class($thumbs_number); ?>">
I even did var_dump()
:
<h2><?php var_dump($thumbs_string); ?></h2>
<h2><?php var_dump($thumbs_number); ?></h2>
and the results were:
string(2) "+1"
and int(1)
respectively (I directly copy/pasted them here).
But no div class is being output.
Any suggestion to fix this?
EDIT:
The class is indeed being output in the HTML source, but it isn't having any effect (and my stylesheet is not being cached). I have another version of the function which doesn't add an extra div around the span tags, and that one works but unfortunately I need that div.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果类名显示在 HTML 中,则 PHP 代码就可以。样式表可能是问题所在。
If the class name is being displayed in the HTML then the PHP code is fine. The stylesheet is likely the problem.
当然..在调用
rating_class($thumbs_number);
之前添加一个echo
:Sure.. add an
echo
before the call torating_class($thumbs_number);
:为什么要使用仅用于回显的额外函数?以下应该可以正常工作。
Why are you using an extra function that's only used to echo? The following should work just fine.