Home » , » Remove a specific tag from php string

Remove a specific tag from php string

Written By Unknown on Kamis, 08 Desember 2011 | 09.47

Sometimes we come to the situation when we need to strip one specific HTML tag from a string and the PHP function strip_tags() doesn’t work the way we want it to.

strip_tags() allows for certain exclusions, but why would we use that when we only want to exclude one tag and include all other tags? Especially when we can use preg_replace().

Here is a quick little function. It takes two parameters, the tag to be removed and the string from which it is to be removed.

  function strip_single($tag,$string){
$string=preg_replace('/<'.$tag.'[^>]*>/i', '', $string);
$string=preg_replace('/<\/'.$tag.'>/i', '', $string);
return $string;
}


Call function like this :

$result = strip_single('a',$string);

It will remove all <a> tags from string.
Share this article :

Posting Komentar

 
Support : Creating Website | Johny Template | Mas Template
Copyright © 2011. Kumpulan Kata Broadcast Blackberry - All Rights Reserved
Template Created by Creating Website Published by Mas Template
Proudly powered by Blogger