tags:

views:

51

answers:

2

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  A: 

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';
}
Phillip Harrington
A: 

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.

matsolof