Several ways to get the numbers in a string by combining regular PHP

Source: Internet
Author: User
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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.