PHP regular expression match gives error

150 Views Asked by At

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?

2

There are 2 best solutions below

0
On BEST ANSWER

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';
}
0
On

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.