PHP combines the regular get string in the digital _php technique

Source: Internet
Author: User

PHP combines regular get number in string

<?php
  $patterns = "/\d+/";//First type
  //$patterns = "/\d/";  The second kind 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] =>
      [2] =>
      [3] =>)
)
//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 = "123 of times 12345678 occur"; Give a function to crawl to the number 12345678 in $a, if not, then return 0
To write a good function,
1. Match only digits of length 8
2. If found, return to change the number, if not then return 0

function A ($STR) {return
Preg_match ('/([0-9]{8})/', $STR, $a)? $a [1]: 0;
}

Search on the Internet to this test can also be found, but if the character length is not d{4} indefinite length is not

function FindNum ($str = ') {
 if (empty ($STR)) {return ';}
 $reg = '/(\d{4} (\.\d+)?) /is ';//matching 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];

Found that this can get any number of consecutive lengths, of course, also see a lot of online, but there is a can, so there is no more testing.

The above mentioned is the entire content of this article, I hope you can enjoy.

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.