1

I have a string and I want to get only number witch is between 5 and 7 charts. Here my problem:

$string = "Test 1 97779 test";
if(strlen(preg_replace("/[^0-9]/", "", $string)) >= 5 && strlen(preg_replace("/[^0-9]/", "", $parts[7])) <= 7) {
    $var = preg_replace("/[^\d-]+/", "", $string);
} 

Result is: 19779, but I want only 97779. If someone have any suggestion I will be very glad. Thanks in advance.

diank
  • 600
  • 2
  • 8
  • 19

1 Answers1

4

Your friend is preg_match

if(preg_match('/\b\d{5,7}\b/', $str, $out))
  $var = $out[0];

See demo at eval.in

bobble bubble
  • 11,968
  • 2
  • 22
  • 34