0

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?

hakre
  • 193,403
  • 52
  • 435
  • 836

2 Answers2

1

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);
Explosion Pills
  • 188,624
  • 52
  • 326
  • 405
  • Note, however, that preg_replace changes the sort of the parameters: It should be `preg_replace('@^.*?(?=\"created_at)@i', 'something', $temp);`. Took me a whole while to figure out what was happening since this proposal wasn't working. – Jorge Antonio Díaz-Benito May 08 '13 at 16:34
  • 1
    @JorgeAntonioDíaz-Benito fixed issue; next time it would help if you include the string to test with. – Explosion Pills May 08 '13 at 17:15
1

As other people have pointed out, ereg functions are deprecated, so use preg_replace. You also have to encapsulate your regex string in slashes (/). You can put your regex flags after your last slash.

Esben Tind
  • 885
  • 4
  • 14