php 基于整数的 if 语句不显示结果?

发布于 2024-11-30 19:05:14 字数 1599 浏览 1 评论 0原文

我正在使用以下 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

诗笺 2024-12-07 19:05:14

如果类名显示在 HTML 中,则 PHP 代码就可以。样式表可能是问题所在。

If the class name is being displayed in the HTML then the PHP code is fine. The stylesheet is likely the problem.

唠甜嗑 2024-12-07 19:05:14

当然..在调用 rating_class($thumbs_number); 之前添加一个 echo

<div class="topic-like-count<?php echo rating_class($thumbs_number); ?>">

Sure.. add an echo before the call to rating_class($thumbs_number);:

<div class="topic-like-count<?php echo rating_class($thumbs_number); ?>">
ぃ双果 2024-12-07 19:05:14

为什么要使用仅用于回显的额外函数?以下应该可以正常工作。

<div class="topic-like-count<?=get_rating_class($thumbs_number);?>">

Why are you using an extra function that's only used to echo? The following should work just fine.

<div class="topic-like-count<?=get_rating_class($thumbs_number);?>">
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文