0

I've a php string like this:

I am a 
  <blockquote> <strong>string</strong> 
    <blockquote> with lot of</blockquote>
  in <a href="#">it</a>
  </blockquote>
hiya!

And I need to remove all blockquotes for transform it in:

I am a
hiya!

I think regex can be usefull but I can't find anything on stackoverflow nor on google and I don't know how to write by myself that.

Can someone tell me how to do it using php?

(I found this, but isn't regex and I don't know if it can remove html into the element too. Using PHP to remove a html element from a string)

Community
  • 1
  • 1
Fez Vrasta
  • 14,110
  • 21
  • 98
  • 160
  • 2
    [Have you tried an HTML parser?](http://stackoverflow.com/a/1732454/476) – deceze Feb 15 '13 at 15:00
  • @deceze do you mean something like simple_html_dom? – Fez Vrasta Feb 15 '13 at 15:00
  • [take a look here](http://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags) and read first answer :) – DonCallisto Feb 15 '13 at 15:01
  • 1
    **TH̘Ë͖́̉ ͠P̯͍̭O̚​N̐Y̡ H̸̡̪̯ͨ͊̽̅̾̎Ȩ̬̩̾͛ͪ̈́̀́͘ ̶̧̨̱̹̭̯ͧ̾ͬC̷̙̲̝͖ͭ̏ͥͮ͟Oͮ͏̮̪̝͍M̲̖͊̒ͪͩͬ̚̚͜Ȇ̴̟̟͙̞ͩ͌͝S̨̥̫͎̭ͯ̿̔̀ͅ** (but you get a + for not insisting on regex like most of these questions) – Fabian Schmengler Feb 15 '13 at 15:02
  • Ok thank you, I'll use it. – Fez Vrasta Feb 15 '13 at 15:06

1 Answers1

3

try this

 $out=preg_replace("~<blockquote(.*?)>(.*)</blockquote>~si","",' '.$str.' ');
mohammad mohsenipur
  • 3,218
  • 2
  • 17
  • 22
  • Thank you, it works, but I don't know if other users who told me to use an HTML parser are right, even if my string is generated by Markdown, so without particolar HTML tags and mistakes... – Fez Vrasta Feb 15 '13 at 15:19
  • @FezVrasta I didn't work with Markdown if Markdown can export as DOMDocument use HTML parser if not Regex is better and faster. – mohammad mohsenipur Feb 15 '13 at 15:23
  • Keep in mind this regex won't work if you have multiple blockquote blocks which are not nested, because this uses greedy matching `.*`. You could use non-greedy matching `.*?`, but then it won't work on nested blockquotes. All in all, I don't think regex is good solution here if you want it to work in all conditions. – Hrvoje Matić Aug 12 '20 at 13:39