T
T
Theory Theory2020-07-21 14:47:23
PHP
Theory Theory, 2020-07-21 14:47:23

Why does an empty regex always return true?

$uri = 'contacts';

var_dump(preg_match('//', $uri)); // true

Но ведь строка не пустая

Answer the question

In order to leave comments, you need to log in

1 answer(s)
R
Rsa97, 2020-07-21
@Narbek

Because this regular expression is treated as "the string contains an empty substring" and is true for any string. The expression "string is empty" looks like /^$/, but it's much easier to check with the usual equality ($url === '').

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question