Public feed #886907

Tag Contents Functions

public php v1 · immutable
Raw Edit
·published 2008-02-01 00:50 UTC
rendered paste body
<?php// 18 lines// Usage:// regex($string, "<title>", "</title>);function regex($string, $start, $end, $strip=TRUE){	/* Sanity Check */	if(is_array($string) OR !isset($string)){		return FALSE; // If its an array, we can't parse it	} else { // If its not an array, go ahead	    // Descide weather to strip out odd chars		if($strip == 1){ // TRUE			$string = str_replace("\n","",str_replace("\r","",$string)); // strip new lines		} elseif($strip == 2){			$string = stripslashes(str_replace("\n","",str_replace("\r","",trim($matches[1],"\x00..\x1F")))); // strip anything		}  // or FALSE or 0	    $break_one=explode($start,$string);	    $break_two=explode($end,$break_one[1]);		return (!$strip == FALSE) ? trim($break_two[0]) : $break_two[0];	} // end if}// 13 lines// Usage:// regex($string, "<title>");function get_tag_contents($string, $tag, $strip=TRUE){	/* Sanity Check */	$tag = str_replace(array("<",">","/"),"",$tag); // Stip out tag parts, we add them in later	if(is_array($string) OR !isset($string)){		return FALSE; // If its an array, we can't parse it	} else { // If its not an array, go ahead			if(!preg_match("/.<".$tag."[^>]*>(.*?)<.".$tag.">/si",$string,$matches) && !is_array($matches)){				return FALSE;			}			// strip extra line slashes out if wanted.			return ($strip) ? stripslashes(str_replace("\n","",trim($matches[1],"\x00..\x1F"))) : $matches[1];	}}function get_all_tag_contents($string, $tag, $strip=TRUE, $multi=FALSE){	/* Sanity Check */	$tag = str_replace(array("<",">","/"),"",$tag); // Stip out tag parts, we add them in later	if(is_array($string) OR !isset($string)){		return FALSE; // If its an array, we can't parse it	} else { // If its not an array, go ahead			if($multi){ // Search for the same tag or not				if(!preg_match_all("/<".$tag."[^>]*>(.*?)<.".$tag.">/s",$string,$matches) && !is_array($matches)){					return FALSE; // We don't have an array. Cannot continue				}				if(count($matches) > 2){					$results = ($strip) ? stripslashes(str_replace("\n","",trim($matches[0][1],"\x00..\x1F"))) : $matches[0][1]; // Get result since we only have one				} else {					$results = array();					foreach($matches[1] as $result){ // This seems to work, leave it for now						if($strip){							$results[] = stripslashes(str_replace("\n","",trim($result,"\x00..\x1F")));						} else {							$results[] = $result;						} // end if					} // end foreach				}			return $results; // if its multi mode, we are done		} else { // if we are not doing multi mode, we start now			if(!preg_match("/.<".$tag."[^>]*>(.*?)<.".$tag.">/si",$string,$matches) && !is_array($matches)){				return FALSE;			}			// strip extra line slashes out if wanted.			return ($strip) ? stripslashes(str_replace("\n","",trim($matches[1],"\x00..\x1F"))) : $matches[1];		}	} // end sanity check} // end function?>