0

I tried:

echo preg_replace('<!--hello//-->','','<p>This is some text<!--hello//-->This is some more text</p>');

Trying to return

<p>This is some textThis is some more text</p>

But instead I got

<p>This is some text<>This is some more text</p>

Why do the brackets remain in the string?

Thanks for the help!

Edit: This question is not looking for the right regex to use...it's moreso why the function preg_replace is behaving weirdly.

8
  • 1
    You need to add regex delimiters, '~<!--hello//-->~'. Or just use str_replace if you plan to remove a fixed substring. See ideone.com/OlAXUY. Commented Jan 8, 2018 at 14:41
  • Thanks @WiktorStribiżew! What is the purpose of those delimiters? Commented Jan 8, 2018 at 14:47
  • Regex delimiters show the boundaries between the pattern and the modifiers (flags, or options). Commented Jan 8, 2018 at 14:48
  • 1
    In your case, the problem is that leading/trailing < and > are parsed as paired regex delimiters. Thus, you need to use more common ones, / or ~, in addition to your pattern. Commented Jan 8, 2018 at 14:50
  • Ahhhh now I understand! I didn't realize that < and > were being treated as delimiters Commented Jan 8, 2018 at 14:53

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.