UTF-8 十六进制通过 mb_substr 算作单个字符吗?
首先 - 我是一个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
将 html 实体解码回正常
http://php.net/manual/en /function.html-entity-decode.php
Decode html entites back to normal
http://php.net/manual/en/function.html-entity-decode.php