Get the App
SLTechnology News&Howtos  ›  Development  › 

How php filters strings to get only numbers

Shulou Source: shulou.com Published: 2022-06-03 07:20:48 10月18日 Update

This article will explain in detail how php filters strings to get only numbers. The editor thinks it is very practical, so I share it for you as a reference. I hope you can get something after reading this article.

Get method: 1. Use the for statement to loop through the string, use the is_numeric () function to extract the numeric characters in the body of the loop and use the ". =" operator to assemble a new string. 2. Using the rule, use the "preg_replace (" / [^ 0-9] / ",", $str) statement.

Operating environment of this tutorial: windows7 system, PHP7.1 version, DELL G3 computer

Method 1: using the is_numeric () function

Use the for loop to iterate over the string $str, and use the is_numeric () function to determine whether $str [$I] is a numeric character. The is_numeric () function can detect whether a variable is a number or a numeric string.

Use is_numeric ($str [$I]) in the body of the loop to determine whether the $str [$I] character is a numeric character; if so, take it out and use ". =" to concatenate a numeric substring.

Look at the output:

01234567890

Method 2: use the preg_replace () function

This is the end of this article on "how php filters strings to get only numbers". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, please share it for more people to see.

Tags: Characters numbers strings functions loops methods articles loop bodies more statements good practical numbers content variables words operators tutorials articles rules Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno OPPO Reno Apple Microsoft Xiaomi Docker