PHP converts English numbers to Arabic numerals, such as zero;three;five;six;eight;one is converted to 035681. Then we can achieve it through the explode(), trim() function, foreach and Switch statements in PHP.
# Below we will introduce to you with specific code examples how to convert English numbers to Arabic numerals in PHP.
Code examples are as follows:
<?php function word_digit($word) { $warr = explode(';',$word); $result = ''; foreach($warr as $value){ switch(trim($value)){ case 'zero': $result .= '0'; break; case 'one': $result .= '1'; break; case 'two': $result .= '2'; break; case 'three': $result .= '3'; break; case 'four': $result .= '4'; break; case 'five': $result .= '5'; break; case 'six': $result .= '6'; break; case 'seven': $result .= '7'; break; case 'eight': $result .= '8'; break; case 'nine': $result .= '9'; break; } } return $result; } echo word_digit("zero;three;five;six;eight;one")."\n"; echo word_digit("seven;zero;one")."\n";
Output:
035681 701
Related functions:
##explode() means using a String splits another string
trim() means removing blank characters (or other characters) at the beginning and end of the string
Related statements:foreach syntaxThe structure provides a simple way to traverse the array. foreach can only be applied to arrays and objects. If you try to apply it to variables of other data types, or uninitialized variables, an error message will be issued.
switch statement is similar to a series of if statements with the same expression. There are many situations where you need to compare the same variable (or expression) with many different values and execute different code depending on which value it equals. This is exactly what the switch statement is for.
This article is about the method of converting English numbers to Arabic numerals in PHP. I hope it will be helpful to friends in need!The above is the detailed content of Convert English numbers to Arabic numbers with PHP. For more information, please follow other related articles on the PHP Chinese website!