Remove html tags from a string wtih PHP

Blog / PHP ·

Sometimes you don't need any formatting. That's just a nice way to remove all the html tags.

Remove all tags:

    function removeTags($str) {  
        $str = preg_replace("#<(.*)/(.*)>#iUs", "", $str);
        return $str;
    }
    

Remove a specific tag:

    function removeTag($str, $tag) { 
        $str = preg_replace("#\\<".$tag."(.*)/".$tag.">#iUs", "", $str);
        return $str;
    }

Krasimir Tsonev With over two decades of deep programming expertise, I offer comprehensive web consultancy and stack audits, alongside specialized workshops, training, and engaging public speaking to elevate your team's skills and optimize your digital presence. Contact me.

Keywords: str tag remove str return str ius str return
Share the post on Twitter, Facebook, LinkedIn