Home  >  Article  >  Backend Development  >  PHP combines regular expressions to get numbers in a string_PHP tutorial

PHP combines regular expressions to get numbers in a string_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 09:49:541020browse

PHP combines regular expressions to get the numbers in the string

This article mainly introduces several methods of PHP combining regular expressions to get the numbers in the string. It is very simple and practical. Friends in need can refer to it.

php combines regular expression to get the numbers in the string

 ?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

$patterns = "/d /"; //第一种

//$patterns = "/d/"; //第二种

$strs="left:0px;top:202px;width:90px;height:30px";

preg_match_all($patterns,$strs,$arr);

print_r($arr);

/***************运行结果*********************/

//第一种

Array

(

[0] => Array

(

[0] => 0

[1] => 202

[2] => 90

[3] => 30

)

)

//第二种

Array

(

[0] => Array

(

[0] => 0

[1] => 2

[2] => 0

[3] => 2

[4] => 9

[5] => 0

[6] => 3

[7] => 0

)

)

1

2

3

4

5

6

1

2

3

function a($str){

return preg_match('/([0-9]{8})/',$str,$a) ? $a[1] : 0;

}

7

8

1

2

3

4

5

6

7

8

9

function findNum($str=''){

if(empty($str)){return '';}

$reg='/(\d{4}(\.\d )?)/is';//匹配数字的正则表达式

preg_match_all($reg,$str,$result);

if(is_array($result)&&!empty($result)&&!empty($result[1])&&!empty($result[1][0])){

return $result[1][0];

}

return '';

}

9

10

11

1

2

$str=trim($str);

if (preg_match('|(\d )|',$str,$r)) return $r[1];

12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
Array ( [0] => 0 [1] => 202 [2] => 90 [3] => 30 ) ) //Second type 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="123 happened in the era 12345678 happened"; Give a function to capture the number 12345678 in $a, if not, return 0 To write good functions, 1. Only match numbers with a length of 8 digits 2. If found, return the changed number, if not, return 0  ?
1 2 3 function a($str){ return preg_match('/([0-9]{8})/',$str,$a) ? $a[1] : 0; }
I searched for this on the Internet and tested it and found that it is OK, but if the character length is not d{4} or a fixed length, it will not work  ?
1 2 3 4 5 6 7 8 9 function findNum($str=''){ if(empty($str)){return '';} $reg='/(\d{4}(\.\d )?)/is';//Regular expression matching numbers preg_match_all($reg,$str,$result); if(is_array($result)&&!empty($result)&&!empty($result[1])&&!empty($result[1][0])){ return $result[1][0]; } return ''; }
Later I found out that there was one among them  ?
1 2 $str=trim($str); if (preg_match('|(\d )|',$str,$r)) return $r[1];

I found that this can get numbers of any continuous length. Of course, I saw a lot of them on the Internet, but there was one that worked, so I didn’t test it again.

The above is the entire content of this article, I hope you all like it.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/1018923.htmlTechArticlephp combines regular expressions to get numbers in a string. This article mainly gives you a summary of how php combines regular expressions to get numbers in a string. Several methods of numbers, very simple and practical, friends in need...
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