I have the following string = '<strong> <hello> text <http://www.google.com> mera';
What I want is to strip tags, but with the exception of the the tag beginning with http://

I have the following preg_replace
echo preg_replace('#<[^http].*?>#', '', $str);

But this also makes an exception of <hello>.
I am completely stuck.
That is why I ask you.

    strip_tags has an optional second parameter that lets you specify allowable tags.

      Yes, I know.
      But in this case there is no possibility.
      There is no such tag in PHP = <http....>

      But why I want it, is markdown, which can use <http://www.google.com> for making html links.

        I thought the HTML looked weird, but with no mention of markdown in the initial post, I made an assumption. You can try Googling markdown libraries - parsedown talks about using HTML Purifier as a tag whitelist; this may help you get to what you're looking for. Unfortunately, markdown parsing in PHP isn't something I've done extensively and regex is not my strongest area, so I think that's about as helpful as I'm gonna be able to be...

          So, a starting <, up to the corresponding >, as long as the tag doesn't start with http. You want a negative lookahead assertion which says "only match < if it is not followed by http"

          #<(?!http)[^>]+>#

          Thanks Weedpacket. That works alright.

            Write a Reply...