0

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?

Ben James
  • 121,135
  • 26
  • 193
  • 155
James
  • 42,081
  • 53
  • 136
  • 161
  • 2
    Why are you using eregi? From the manual: *This function has been DEPRECATED as of PHP 5.3.0. Relying on this feature is highly discouraged.* http://www.php.net/manual/en/function.eregi.php – Mark Byers Sep 17 '10 at 23:15

2 Answers2

2

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

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
  • 2,665
  • 3
  • 18
  • 17