如何在非英文字符上使用子字符串?

2 php substr htmlspecialchars

我有一个小网站,我在其中显示帖子标题的搜索结果,有些标题长达 255 个字符,在 html 表格中显示这些标题时,表格的换行符即显示不正确,所以我使用substrphp 函数修剪标题,以便它可以适合表格行。

对于英文标题,它工作得很好,但对于非英文标题,它显示空白,即修剪所有内容。

我是substr这样用的

<a href="<? echo $link; ?>" class="strong"><? echo htmlspecialchars(substr($row['title'],0,70)); ?></a>
Run Code Online (Sandbox Code Playgroud)

那么我怎样才能使非英文标题也是 70 个字符呢?

l'L*_*L'l 5

您应该使用multi-byte safe substr()基于 UTF-8 字符数的操作:

mb_substr();
Run Code Online (Sandbox Code Playgroud)

http://php.net/manual/en/function.mb-substr.php