0

Hi I want to replace the content between "<<" and ">>" with custom div class how can I do that?

I tried some regex but It didn't work.

$con = preg_replace ('/<<(.*?)>>/s', '<div>$1</div>', $con);
4
  • 1
    since you already tried some regex, can you show us what you tried? ;) Commented Nov 18, 2015 at 13:36
  • $con = preg_replace ('/<<(.*?)>>/s', '<div>$1</div>', $con); Commented Nov 18, 2015 at 13:38
  • What's the value of $con - I've just literally dropped this into a PHP file and it works : echo preg_replace('/<<(.*?)>>/s', "<div>$1</div>", "<<womble>>"); - it prints out <div>womble</div> Commented Nov 18, 2015 at 13:43
  • Shouldn't make any odds, neither < or > are meta characters in RegExp (except for a lookbehind) so there's no reason why that shouldn't work "as-is" ... strange. Commented Nov 18, 2015 at 13:51

1 Answer 1

3

I would propose putting the < and > in character classes:

preg_replace('/[<]{2}(.*?)[>]{2}/s', '<div>$1</div>', $con);

Escaping them should also work

preg_replace('/\<\<(.*?)\>\>/s', '<div>$1</div>', $con);

But CC001's comment above is correct: Neither one of these is needed.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.