This article mainly introduces the PHP combined with regular to get the number of strings in several ways, interested in the reference of friends, I hope to be helpful to everyone.
PHP combined with regular gets the numbers in the string
<?php $patterns = "/\d+/";//the first type //$patterns = "/\d/"; The second type of $strs = "left:0px;top:202px;width:90px;height:30px"; Preg_match_all ($patterns, $strs, $arr); Print_r ($arr); /*************** Run result *********************///First array ( [0] = = Array ( [0] = 0 [1] = 202 [2] = [3] = +))//The second array ( [0] = = Array ( [0] = 0 [1] = 2 [2] = + 0 [3] = > 2 [4] = 9 [5] = 0 [6] = 3 [7] = 0 ))
PHP gets the number of the specified string $a = "The Age of 123 born of 12345678 occurrences"; Gives a function to fetch the number 12345678 in $ A, and if not, returns 0
To write a good function,
1. Match only digits of length bit 8
2. Returns the number if found, or 0 if not
function A ($STR) {return Preg_match ('/([0-9]{8})/', $STR, $a)? $a [1]: 0;}
Searching for this test on the Internet can also be found, but if the character length is not d{4} not long
function FindNum ($str = ") {if (empty ($STR)) {return ';} $reg = '/(\d{4} (\.\d+)?) /is ';//match number of regular expression Preg_match_all ($reg, $str, $result); if (Is_array ($result) &&!empty ($result) &&!empty ($result [1]) &&!empty ($result [1][0])) { return $result [1][0]; } return ';}
And then I found out that there was a
$str =trim ($STR); if (Preg_match (' | | ( \d+) ($str, $r)) return $r [1];
Find this can get any number of consecutive length of the numbers, of course, there are many on the Internet, but one can be, so there is no further testing.
Summary: The above is the entire content of this article, I hope to be able to help you learn.
Related recommendations:
PHP template and JS upload plug-in combination to implement a non-flush upload method
PHP Digital and digital Operation verification Code
How to implement JSON code conversion in PHP