Search code examples
phpregexpreg-matchextractdigits

php preg_match from sequence of numbers extract 10 digits that starts with 514


from a sequence of numbers I want to extract 10 digits that begins by 514

Example 2134565323551412344558987

With preg_match or any other php code I want to extract the 5141234455

I Tried this code

preg_match_all("/^514/", $contentNumbersFromURL, $matches)

Bu I get

array(1) { [0] => array(0) {} }

Solution

  • You need to remove start of the line anchor ^. ^ matches the line starting boundary. Since there isn't a number 514 exists at the start, your regex got failed.

    preg_match_all('/514\d{7}/', $contentNumbersFromURL, $matches)