################################################################################ # The following scripts are from a series of three articles called: # Pro Parsing Tips with PHP # # These articles are found in php[architect] magazine, 2018. # # Feel free to cut and paste these functions into your own code. # # Please direct your questions and comments to mike@schrenk.com # ################################################################################ /*********************************************************************** remove($string, $open_tag, $close_tag) ------------------------------------------------------------- DESCRIPTION: Removes all text between $open_tag and $close_tag INPUT: $string The target of your parse $open_tag The starting delimitor $close_tag The ending delimitor ***********************************************************************/ function remove($string, $open_tag, $close_tag) { # Get array of things that should be removed from the input string $remove_array = parse_array($string, $open_tag, $close_tag); # Remove each occurrence of each array element from string; for($xx=0; $xx<count($remove_array); $xx++) $string = str_replace($remove_array, "", $string); return $string; } /*********************************************************************** $array = parse_array($string, $open_tag, $close_tag) ------------------------------------------------------------- DESCRIPTION: Returns an array of strings that exists repeatedly in $string. This function is useful for returning an array that contains links, images, tables or any other data that appears more than once. INPUT: $string String that contains the tags $open_tag Name of the open tag (i.e. "<a>") $close_tag Name of the closing tag (i.e. "</title>") ***********************************************************************/ function parse_array($string, $beg_tag, $close_tag) { preg_match_all("($beg_tag(.*)$close_tag)siU", $string, $matching_data); return $matching_data[0]; } /*-------------------------*/ /*********************************************************************** split_string($string, $delineator, $desired, $type) ------------------------------------------------------------- DESCRIPTION: Returns a potion of the string that is either before or after the delineator. The parse is not case sensitive, but the case of the parsed string is not effected. INPUT: $string Input string to parse $delineator Delineation point (place where split occurs) $desired BEFORE: return portion before delineator AFTER: return portion before delineator $type INCL: include delineator in parsed string EXCL: exclude delineator in parsed string ***********************************************************************/ function split_string($string, $delineator, $desired, $type) { # if needed, convert string and delineator to lower case $lc_str = strtolower($string); $marker = strtolower($delineator); # Return text BEFORE the delineator if($desired == BEFORE) { if($type == EXCL) // Return text ESCL of the delineator $split_here = strpos($lc_str, $marker); else // Return text INCL of the delineator $split_here = strpos($lc_str, $marker)+strlen($marker); $parsed_string = substr($string, 0, $split_here); } # Return text AFTER the delineator else { if($type==EXCL) // Return text ESCL of the delineator $split_here = strpos($lc_str, $marker) + strlen($marker); else // Return text INCL of the delineator $split_here = strpos($lc_str, $marker) ; $parsed_string = substr($string, $split_here, strlen($string)); } return $parsed_string; } /*-------------------------*/ /*********************************************************************** $value = return_between($string, $start, $end, $type) ------------------------------------------------------------- DESCRIPTION: Returns a substring of $string delineated by $start and $end The parse is not case sensitive, but the case of the parsed string is not effected. INPUT: $string Input string to parse $start Defines the beginning of the sub string $end Defines the end of the sub string $type INCL: include delineators in parsed string EXCL: exclude delineators in parsed string ***********************************************************************/ function return_between($string, $start, $stop, $type) { $temp = split_string($string, $start, AFTER, $type); return split_string($temp, $stop, BEFORE, $type); } /*-------------------------*/