如何使用php5将整数值转换为word格式?

发布于 2024-11-08 03:11:23 字数 205 浏览 2 评论 0原文

我想将数字转换为word格式。

例如:

$count = 5;
echo "Hello Mr user this is your ".$count." review."

我需要这样的输出......

“用户先生您好,这是您的第五条评论。”

I want to convert a number to word format.

For example:

$count = 5;
echo "Hello Mr user this is your ".$count." review."

I need output like this...

"Hello Mr user this is your fifth review."

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

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

发布评论

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

评论(4

誰認得朕 2024-11-15 03:11:23

在另一个 StackOverflow 线程中检查这一点:

将数字转换为其字符串表示形式

Check this in another StackOverflow thread:

Convert a number to its string representation

薄情伤 2024-11-15 03:11:23

php 中没有内置函数可以实现这一点。但尝试外部库,例如: http://pear.php.net/package/Numbers_Words

There is no built in function in php to make that. But try external lib like : http://pear.php.net/package/Numbers_Words

凉墨 2024-11-15 03:11:23

如果您指的是内置函数,则不,PHP 没有内置函数。但您可以创建自己的。

如果您只查看英文数字,​​那么这不是什么大问题。但是,如果您需要处理外语(例如阿拉伯语),则必须做一些额外的工作,因为数字和物体都有性别。所以算法变得有点复杂。

郑重声明一下,我正在开发一个阿拉伯语开源转换工具。

If you're referring to a built-in function, no, PHP does not have one. But You can create your own.

If you're looking at only English numbers, it's not much of an issue. But if you need to deal with a foreign language (like Arabic), you have to do a bit of extra work since numbers and objects have genders. So the algorithm gets a little more complex.

Just for the record, I'm working on producing an open source conversion tool for Arabic.

遗失的美好 2024-11-15 03:11:23

我知道这并不完全是你想要的,但我从 php.net 上某个地方的评论中找到了这个(找不到源代码),它适用于像 1st, 第 243 位第 85 位

function ordinalize($num)
{
    if ( ! is_numeric($num)) return $num;

    if ($num % 100 >= 11 and $num % 100 <= 13)
    {
        return $num."th";
    }
    elseif ( $num % 10 == 1 )
    {
        return $num."st";
    }
    elseif ( $num % 10 == 2 )
    {
        return $num."nd";
    }
    elseif ( $num % 10 == 3 )
    {
        return $num."rd";
    }
    else
    {
        return $num."th";
    }
}

为了可读性和简单性,您甚至可能想考虑这种格式,具体取决于您期望数字有多高。如果它们少于 100,您应该能够轻松编写自己的函数。然而,如果他们能变得非常高:

“用户先生您好,这是您的第 3425 条评论。”

听起来有点尴尬:)

希望这对一些人有帮助。

I know this isn't exactly what you're after, but I picked this up from the comments on php.net somewhere (can't find source), it works for output like 1st, 243rd and 85th.

function ordinalize($num)
{
    if ( ! is_numeric($num)) return $num;

    if ($num % 100 >= 11 and $num % 100 <= 13)
    {
        return $num."th";
    }
    elseif ( $num % 10 == 1 )
    {
        return $num."st";
    }
    elseif ( $num % 10 == 2 )
    {
        return $num."nd";
    }
    elseif ( $num % 10 == 3 )
    {
        return $num."rd";
    }
    else
    {
        return $num."th";
    }
}

You might want to even consider this format anyways for readability and simplicity, depending on how high you expect the numbers to be. If they are less than 100, you should be able to write your own function easily. However, if they can get really high:

"Hello Mr user this is your three thousand four hundred and twenty-fifth review."

Sounds a bit awkward :)

Hope this helps some.

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