Home  >  Article  >  Backend Development  >  How to convert words to numbers in PHP

How to convert words to numbers in PHP

藏色散人
藏色散人Original
2018-12-27 15:35:562923browse


PHP将单词转换为数字,如单词zero、three、five、six、eight、one转换为数字035681。我可以通过foreach和switch循环来实现。

How to convert words to numbers in PHP

下面我们就结合具体的代码示例,给大家介绍PHP将单词转换为数字的实现方法。

代码示例如下:

";
echo word_digit("seven;zero;one")."
";

输出结果如下:

How to convert words to numbers in PHP

相关函数介绍:

explode函数表示使用一个字符串分割另一个字符串。

说明 :

array explode ( string $delimiter , string $string [, int $limit ] )

此函数返回由字符串组成的数组,每个元素都是 string 的一个子串,它们被字符串 delimiter 作为边界点分割出来。

trim函数表示去除字符串首尾处的空白字符(或者其他字符)。

说明 :

string trim ( string $str [, string $character_mask = " \t\n\r\0\x0B" ] )

此函数返回字符串 str 去除首尾空白字符后的结果。如果不指定第二个参数,trim() 将去除这些字符:

.=是PHP 字符串运算符之一,表示串接赋值,即字符串连接。(如:$txt1 = "Hello" $txt1 .= " world!"    现在 $txt1 包含 "Hello world!"   ) 

foreach 语法结构提供了遍历数组的简单方式。foreach 仅能够应用于数组和对象,如果尝试应用于其他数据类型的变量,或者未初始化的变量将发出错误信息。

switch 语句类似于具有同一个表达式的一系列 if 语句。很多场合下需要把同一个变量(或表达式)与很多不同的值比较,并根据它等于哪个值来执行不同的代码。这是 switch 语句的用途。

本篇文章就是关于PHP将单词转换为数字的具体方法介绍,希望对需要的朋友有所帮助!


The above is the detailed content of How to convert words to numbers in PHP. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn