Convert English numbers to Arabic numbers with PHP

藏色散人
Release: 2019-01-29 10:31:40
Original
7460 people have browsed it

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.

Convert English numbers to Arabic numbers with 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(&#39;;&#39;,$word);
    $result = &#39;&#39;;
    foreach($warr as $value){
        switch(trim($value)){
            case &#39;zero&#39;:
                $result .= &#39;0&#39;;
                break;
            case &#39;one&#39;:
                $result .= &#39;1&#39;;
                break;
            case &#39;two&#39;:
                $result .= &#39;2&#39;;
                break;
            case &#39;three&#39;:
                $result .= &#39;3&#39;;
                break;
            case &#39;four&#39;:
                $result .= &#39;4&#39;;
                break;
            case &#39;five&#39;:
                $result .= &#39;5&#39;;
                break;
            case &#39;six&#39;:
                $result .= &#39;6&#39;;
                break;
            case &#39;seven&#39;:
                $result .= &#39;7&#39;;
                break;
            case &#39;eight&#39;:
                $result .= &#39;8&#39;;
                break;
            case &#39;nine&#39;:
                $result .= &#39;9&#39;;
                break;
        }
    }
    return $result;
}

echo word_digit("zero;three;five;six;eight;one")."\n";
echo word_digit("seven;zero;one")."\n";
Copy after login

Output:

035681                                                                 
701
Copy after login

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!

source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!