Home Daily Programming PHP Knowledge Convert English numbers to Arabic numbers with PHP

Convert English numbers to Arabic numbers with PHP

Jan 28, 2019 pm 02:00 PM

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!

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

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

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)