开发者

PHP regular expression match gives error [duplicate]

This question already has answers here: 开发者_Python百科 How can I convert ereg expressions to preg in PHP? (4 answers) Closed 3 years ago.

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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜