How to implement php to only take numbers: 1. Use the "preg_match" function to extract numbers from the string; 2. Use PHP's built-in "in_array" method to extract all the numbers that appear in the string. And spliced together; 3. Use the "is_numeric" method.
php Extract numbers from the string
1. Regular extraction [Only extract the first occurrence Numbers]
<?php $str ='我是123456一段测试的字789符串00'; preg_match('/\d+/',$str,$number); print_r($number); ?>
2. Regular extraction [All numbers appearing in the string are extracted in array form]
<?php $str ='我是123456一段测试的字789符串00'; preg_match_all('/\d+/',$str,$arr); print_r($arr); ?>
3. Use in_array Method [Extract all the numbers that appear in the string and splice them together in string form] Pay attention to the encoding issue
$str='我是123456一段测试的字789符串00'; $str=trim($str); if (!empty($str)) { $key = ['0','1','2','3','4','5','6','7','8','9']; $number = ''; for ($i = 0;$i < strlen($str);$i++) { if (in_array($str[$i],$key)){ $number .= $str[$i]; } } } print_r($number);
4. Use the is_numeric method like the in_array method
The above is the detailed content of How to implement the function of taking only numbers in php. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

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

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

Atom editor mac version download
The most popular open source editor

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.
