0

I have been working on this for some time. As I researched the PHP warning, I believe the following PHP code requires delimiters at the last half to make it work.

Here is the code,

$pee = preg_replace('!<p><!--(.*?)--></p>!ise', " '<!--' .  stripslashes(clean_pre('$1'))  . '-->' ", $pee);

This is the warning I get,

Warning: preg_replace(): Unknown modifier '-'

I found that when I put the latter half of the code on a new line, that is where the warning refers, so its the latter half that needs the syntax fixed.

It appears the dashes are considered as modifiers, so apparently using delimiters will correct that. I tried to add delimiters, but each failed and the warning was not resolved.

I am rather new at PHP, your help is appreciated.

Thanks

chris85
  • 23,846
  • 7
  • 34
  • 51
Mark2090
  • 143
  • 1
  • 8
  • you have delimiters (!) you need escaping –  Nov 27 '17 at 22:39
  • 1
    The `e` modifier also is deprecated/removed depending on your PHP version. You should use `preg_replace_callback`. ("This feature was DEPRECATED in PHP 5.5.0, and REMOVED as of PHP 7.0.0.") -http://php.net/manual/en/reference.pcre.pattern.modifiers.php – chris85 Nov 27 '17 at 22:41
  • To add to @iainn's answer, those double quotes around the second argument shouldn't be there. You can tell because the syntax highlighting is treating the `stripslashes()` as part of the string. – rickdenhaan Nov 27 '17 at 22:43
  • @rickdenhaan Actually that's valid when using the `e` modifier, although as mentioned already, that's deprecated (or entirely removed) behaviour. – iainn Nov 27 '17 at 22:45
  • @iainn We live and learn ;-) I never knew that. – rickdenhaan Nov 27 '17 at 22:46
  • It's reading `!

    <!` as being your expression, so it's saying "who buddy, you have this random modifier `-` that doesn't exist, mind telling me what that is" and bam voila, escape `!` or use a different delimiter like `~`.

    – ctwheels Nov 27 '17 at 22:49

1 Answers1

2

If you're using ! to surround your regex, then you'll also need to escape that character in the pattern:

!<p><!--(.*?)--></p>!ise
     ^ This needs escaping.

You should be able to use this:

$pee = preg_replace('!<p><\!--(.*?)--></p>!ise', " '<!--' .  stripslashes(clean_pre('$1'))  . '-->' ", $pee);

(or just pick a different delimiter (/, etc).

The reason you're getting that error is that - is the first character following !, so PHP is assuming you're providing it as the first modifier.

Edit: As @chris85 mentioned, the e modifier is no longer good practice, see this question for some advice on updating your code: Replace preg_replace() e modifier with preg_replace_callback

iainn
  • 16,826
  • 9
  • 33
  • 40
  • Thanks, that did resolve the warning, and your also correct on the e modifier is also deprecated. The warning changed from what I had to the deprecated warning, to use the "preg_replace_callback" instead. To use "preg_replace_callback" requires 2 arguments, which are missing in that syntax. Any good links to learn how to use "preg_replace_callback" you know of (other than the PHP manual)? – Mark2090 Nov 27 '17 at 22:55