PHP check if string has numbers together

php regex codeigniter expression

542 просмотра

1 ответ

I need to filter a message text, basically the message text shouldn't contain a phone number. which means i need to check if there is a continues pattern or series of numbers in a string. i'm doing this in codeigniter. now i'm using following type of code to do this,

if (preg_match_all( "/[0-9]/", $msg ) >= 7) {
   $status = 'pending';
   $read = 'no';
}

what it does is count all the numeric characters in the message text and if count is greater than 7 its not passing trough. but i need to check if there is a continues number / a phone number. please help.

Автор: Dhananjaya Источник Размещён: 08.11.2019 10:55

Ответы (1)


2 плюса

Решение

Assuming phone number to be of 7 continuous digits at least, you can use negative lookbehind like

^(?!.*\d{7})

Regex Demo

PHP Code

$re = "/^(?!.*\\d{7}).*/m"; 
$str = "1234567\n123456\n12345673536"; 

preg_match_all($re, $str, $matches);
print_r($matches);

Ideone Demo

Автор: rock321987 Размещён: 20.08.2016 05:37
Вопросы из категории :
32x32