PHPBuilder - untag - gets the content from any given tag

RSS Twitter

untag - gets the content from any given tag

by: Michael Baas
April 27, 2005

Version: 1.1

Type: Function

Category: HTML

License: GNU General Public License

Description: This function extracts the content from any given tag and returns it as a string or an array (if there is more than one occurance of the tag in the string). It's a bit like a step toward an xpath functionality and very good for cleaning up HTML and getting XML data.


echo "untag(\$string,\"mode\",0)<br>".untag($string,"mode",0)."<br><br>";
echo "untag(\$string,\"mode\",1)<br><br>";
foreach ($alltags as $disp){
	echo $disp."<br>";

	Function untag($string,$tag,mode){
	written by Chris Heilmann (
	filters the content of tag $tag from $string 
	when mode is 1 the content gets returned as an array
	otherwise as a string
function untag($string,$tag,$mode){
/*	$preg="/<".$tag.>(.*?)<\/".$tag.">/si";
 * 2005-04-27: preg modified by Michael Baas (
 * so that it would also work when parameters are specified within the
 * tag (as in <body bgcolor=..> etc.)
 * Also changed sep-char so that it does not need to be escaped within the string.
 * BTW: I'm quite new to regex and I did this with the help of RegExBuddy
 * which is very helpful to debug+understand regex's :)

       $preg = "|<" . $tag . "[^>]*>(.*?)</" . $tag . ">|si";
	foreach ($tags[1] as $tmpcont){
		if ($mode==1){$tmpval[]=$tmpcont;}
		else {$tmpval.=$tmpcont;}
	return $tmpval;

Comment and Contribute

Your comment has been submitted and is pending approval.

Michael Baas



(Maximum characters: 1200). You have characters left.