Search code examples
phpregexposix-ere

php - valid regEx expression in ereg_replace makes nothing


I'm using PHP 5.2.17. I want to remove some surplus data from a JSON string and I've thought I can use some replace function to do so. Specifically I'm using ereg_replace with the next expression:

'^.*?(?=\"created_at)'

Which I've validated at http://www.regexpal.com. I've pasted my JSON string in there and the match is right. However, when I make the call:

$tweets = eregi_replace('^.*?(?=\"created_at)', $temp, 'something');

and then I echo the $tweets variable, there's output. No errors in console neither. Apache error log, however, complains about an error called REG_BADRPT. There's a comment in the php docs of eregi_replace suggesting this can be due to I need to escape special characters, but I've already escaped the " character. And I've tried to escape others but no different behavior.

Where could the problem be then?


Solution

  • I don't think that ereg supports lookarounds. preg_replace exists in php 5.2, so you should really use that instead. It will work with your expression with delimiters.

    $tweets = preg_replace('@^.*?(?=\"created_at)@i', 'something', $temp);