PHP strlen() 和字符串的宽度

发布于 2024-12-26 02:34:10 字数 317 浏览 3 评论 0原文

不确定是否有 php 函数可以帮助确定这一点。

我有一些带有可变字符的字符串。我的问题是字符串有多长,而不是有多少个字符。

$str1 = '123456789';
$str2 = 'akIOuNBGH';

两者都有 9 个字符。然而,由于字符较宽,第二个在浏览器中稍长一些。

最终,我想在两个字符串后面附加“............”一系列点,但由于第二个和第一个的宽度不同,因此均匀性关闭。

关于如何确定字符串宽度有什么很酷的想法或方法吗? 也许,如果我知道宽度值,我可以适当地对附加信息进行编码。

Not sure if there's a php function that can help determine this.

I have some strings with variable characters. My problem is how long the string is, not how many characters there are.

$str1 = '123456789';
$str2 = 'akIOuNBGH';

Both have 9 characters. However, the second is slightly longer in the browser, due to wider characters.

Ultimately I'd like to append '.............' a series of dots after the two strings, but since the second and first are not the same width, the uniformity is off.

Any cool ideas or ways on how to determine string width?
Perhaps, if I know the width values, I can code the appended information appropriately.

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

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

发布评论

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

评论(2

只是偏爱你 2025-01-02 02:34:10

宽度取决于您在客户端使用的表示这些字符串的字体类型。对于某些字体(不知道到底是哪种,但我认为例如 Courier),宽度与字符数成正比,因此简单的字符串填充就可以了。对于其他字体,它不起作用,并且取决于浏览器如何呈现它。如果您尝试在服务器端对齐字符串,那么您正在以最糟糕的方式进行操作。尝试使用 CSS 对齐它们。

Width depends on the type of font you use on the client side to represent those strings. For some fonts (don't know exactly which, but I think Courier for example), width is directly proportional to the number of characters, so simple padding of your strings will work. For other fonts, it will not work and will depend on how browsers render it. If you are trying to align your strings on the server side, you are doing it in the worst possible way. Try to align them using CSS.

溇涏 2025-01-02 02:34:10

正如 vucetica 提到的等宽字体(例如 CourierCourier New)可以帮助您在字符之间实现相等的间距。

另外,如果您想确保所有字符串的长度相同,例如 32 个字符,您可以使用 sprintf 方法

$string = '1234567890';
$string = sprintf("%'.-32s", $string);
var_dump($string); //string(32) "1234567890......................"

更多语法示例可以在 PERL 中找到 文档,其中 90% 在 php 中运行

As vucetica mentioned monospaced fonts like Courier or Courier New could help you to archive equal spacing between characters.

Also if you want to make sure that all strings are same length for example 32 characters you can use sprintf method

$string = '1234567890';
$string = sprintf("%'.-32s", $string);
var_dump($string); //string(32) "1234567890......................"

More syntax examples can be found in PERL documentation, 90% of which works in php

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