search
HomeDaily ProgrammingPHP KnowledgeConvert English numbers to Arabic numbers with PHP

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";

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!

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.