2

I got the following string:

last_name, first_name
bjorge, philip
kardashian, [kghim]
mer#$##Code:menu:51587daa7030e##$#cury some more
data #$##Code:menu:515r4387daa7dsf030e##$#, freddie

im trying to replace the Codes in the middle with the function: 'codeParser' the regex is:

$PC_File = preg_replace_callback("(?=\#\$\#\#).*?(?<=\#\#\$\#)", 'codeParser', $PC_File);

but getting this error:

PHP Warning:  preg_replace_callback() : Unknown modifier '.'
Mike
  • 741
  • 13
  • 40
  • 2
    @Ashish I'm glad that's only a comment and not an answer :P – AD7six Apr 01 '13 at 15:56
  • Now that the delimiter problem has been solved, let's talk about those lookarounds. `'/#\$##(.*?)##\$#/'` matches the same things your regex does, but much more efficiently. The capturing group allows you to access the `Code` value directly (via `$matches[1]`), so you don't have to strip off the surrounding stuff in a separate step. – Alan Moore Apr 01 '13 at 18:29

1 Answers1

7

You need to wrap your regular expression in delimiters. It's considering () to be the delimiters right now, and the . as a modifier (which is of course invalid).

"/(?=#\\$##).*?(?<=##\\$#)/"

(I'm also pretty sure the # do not need to be escaped unless you were using them as delimiters)

EDIT: You need \\ to properly escape the $ in double-quotes.

Explosion Pills
  • 188,624
  • 52
  • 326
  • 405
  • The error is gone but still it is not calling the function, in an online regex its working, maybe preg_replace_callback isnt good for php 4.39? – Mike Apr 01 '13 at 15:56
  • 1
    Additional explanation: you can use `(` as start delimiter but then the end delimiter needs to be `)`. If you do so, the expression is just `(?=#\$##)` and `.*?(?<=##\$#)` become the flags/modifiers. Thus the error. – Álvaro González Apr 01 '13 at 15:57
  • @ÁlvaroG.Vicario correct, and I think you can also use `[]` and `{}` for delimiters in the same way. – Explosion Pills Apr 01 '13 at 15:58
  • @user1725378 what does `preg_last_error` say? – Explosion Pills Apr 01 '13 at 15:59
  • i put: file_put_contents("getFilelog.txt","error:".preg_last_error()."\n" ,FILE_APPEND); after the regex.. file say error:0 – Mike Apr 01 '13 at 16:01
  • 100%! its a copy paste. – Mike Apr 01 '13 at 16:03
  • because in online regex its working, you think maybe my server dont support something here? or i got a mistake here? – Mike Apr 01 '13 at 16:05
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/27338/discussion-between-user1725378-and-explosion-pills) – Mike Apr 01 '13 at 16:05