UTF-8 十六进制通过 mb_substr 算作单个字符吗?

发布于 2024-12-10 18:19:55 字数 1045 浏览 2 评论 0原文

首先 - 我是一个 php 新手。我试图使用 mb_substr 限制 WordPress 主题上标题的长度,但当标题中存在某些符号(例如“'”(撇号)或“-”(破折号))时,它返回的字符会更少。

这是我正在使用的代码,将字符总数限制为 60 个(忽略非省略号):

    <?php 
        $short_title = the_title('','',false);
        $short_title_2 = mb_substr($short_title,0,60, 'utf-8');?>
    <h3>
    <a href="<?php the_permalink(); ?>">

            <?php echo $short_title_2; if($short_title_2!=$short_title) { echo "..."; }; ?>
    </a>
</h3>

所以基本上我不希望它返回截断为 60 个字符的标题,但是当我有任何形式的标点符号或其他特殊字符,它会将它们计为单独的 6 个字符(必须计算它们的 unicode 值或其他字符?),这意味着它实际上只会返回 54 个字符。

这是带有破折号字符的示例标题:

Competition - Win Tees from Listen To Your Eyes Clothing Now Ended

代码应该返回:

<h3>Competition - Win Tees from Listen To Your Eyes Clothing Now…</h3>

它实际返回的内容:

<h3>Competition – Win Tees from Listen To Your Eyes Clothi…</h3>

数据库字符集设置为 utf8_general_ci (包括标题表)

有什么方法可以克服这个问题吗?

First off - I'm a php novice. I'm trying to limit the length of titles on a wordpress theme using mb_substr but it's returning fewer characters when there are certain symbols within the title such as "'" (apostraphe) or "-" (dash).

Here is the code I'm working with, limiting the characters to 60 in total (ignore the non-ellipsis):

    <?php 
        $short_title = the_title('','',false);
        $short_title_2 = mb_substr($short_title,0,60, 'utf-8');?>
    <h3>
    <a href="<?php the_permalink(); ?>">

            <?php echo $short_title_2; if($short_title_2!=$short_title) { echo "..."; }; ?>
    </a>
</h3>

So basically I wan't this to return the title truncated to 60 characters, but when I have any form of punctuation or other special characters it counts them as a separate 6 characters (must be counting their unicode value or something?) meaning it will actually only return 54 characters.

Here's and example title with dash character:

Competition - Win Tees from Listen To Your Eyes Clothing Now Ended

The code should return:

<h3>Competition - Win Tees from Listen To Your Eyes Clothing Now…</h3>

What it actually returns:

<h3>Competition – Win Tees from Listen To Your Eyes Clothi…</h3>

The database charset is set to utf8_general_ci (including the table for the title)

Is there any way I can overcome this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

美人迟暮 2024-12-17 18:19:55

将 html 实体解码回正常

$short_title_2 = mb_substr(html_entity_decode($short_title, ENT_QUOTES),0,60, 'utf-8');

http://php.net/manual/en /function.html-entity-decode.php

Decode html entites back to normal

$short_title_2 = mb_substr(html_entity_decode($short_title, ENT_QUOTES),0,60, 'utf-8');

http://php.net/manual/en/function.html-entity-decode.php

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文