PHP regular expression match gives error [duplicate]
Trying to check the string for pattern.
$variable = '[text]';
if (eregi("/(\[(.*?)\])/", $variable)) {}
This code gives error eregi() [function.eregi]: REG_BADRPT
What is the solution for this?
It's because you're using a preg style expression in eregi. You don't need the perl style delimiters.
However, as Mark Byers comments, using preg_match is future proof.
<?php
$variable = '[text]';
if (preg_match("/(\[(.*?)\])/", $variable)) {
echo 'ok';
}
Just to clarify, pearl style delimeters are the two slashes. This is what ereg syntax looks like:
<?php
$str = 'abc';
if (ereg('a', $str))
{
echo 'match found'; // match found
}
?>
I didn't use a regular expression, as you normally would, just to keep things simple.
I also want to mention that there are multibyte ereg functions that are still valid, for example mb_ereg.
精彩评论