<?php /*
#################################################################
Copyright 2007, Michael Schrenk This software is designed for use with the book, "Webbots, Spiders, and Screen Scarpers", Michael Schrenk, 2007 No Starch Press, San Francisco CA W3C� SOFTWARE NOTICE AND LICENSE This work (and included software, documentation such as READMEs, or other related items) is being provided by the copyright holders under the following license. By obtaining, using and/or copying this work, you (the licensee) agree that you have read, understood, and will comply with the following terms and conditions. Permission to copy, modify, and distribute this software and its documentation, with or without modification, for any purpose and without fee or royalty is hereby granted, provided that you include the following on ALL copies of the software and documentation or portions thereof, including modifications: 1. The full text of this NOTICE in a location viewable to users of the redistributed or derivative work. 2. Any pre-existing intellectual property disclaimers, notices, or terms and conditions. If none exist, the W3C Software Short Notice should be included (hypertext is preferred, text is permitted) within the body of any redistributed or derivative code. 3. Notice of any changes or modifications to the files, including the date changes were made. (We recommend you provide URIs to the location from which the code is derived.) THIS SOFTWARE AND DOCUMENTATION IS PROVIDED "AS IS," AND COPYRIGHT HOLDERS MAKE NO REPRESENTATIONS OR WARRANTIES, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO, WARRANTIES OF MERCHANTABILITY OR FITNESS FOR ANY PARTICULAR PURPOSE OR THAT THE USE OF THE SOFTWARE OR DOCUMENTATION WILL NOT INFRINGE ANY THIRD PARTY PATENTS, COPYRIGHTS, TRADEMARKS OR OTHER RIGHTS. COPYRIGHT HOLDERS WILL NOT BE LIABLE FOR ANY DIRECT, INDIRECT, SPECIAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF ANY USE OF THE SOFTWARE OR DOCUMENTATION. The name and trademarks of copyright holders may NOT be used in advertising or publicity pertaining to the software without specific, written prior permission. Title to copyright in this software and any associated documentation will at all times remain with copyright holders.
#################################################################
*/
#################################################################
# # LIB_parse.php Parse Routines # #----------------------------------------------------------------------- # FUNCTIONS # # split_string() Returns the portion of a string either before # or after a delineator. The returned string may # or may not include the delineator. # # return_between() Returns the portion of a string that falls # between two delineators, exclusive or inclusive # of the delineators. # # parse_array() Returns an array containing all occurrences of # text that falls between a set of delineators. # # get_attribute() Returns the value of a HTML tag attribute # # remove() Removes all occurrences of a string from # another string. # # tidy_html() Puts raw HTML into a known state with proper # with parsable syntax #
#################################################################
********************************************************************* Parse Constants (scope = global) ----------------------------------------------------------------------
# Specifies if parse includes the delineator define("EXCL", true); define("INCL", false); # Specifies if parse returns the text before or after the delineator define("BEFORE", true); define("AFTER", false);
********************************************************************* split_string(delineator, 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: delineator Delineation point (place where split occurs) type INCL: include delineator in parsed string EXCL: exclude delineator in parsed string *********************************************************************
function split_string(delineator, type) { # Case insensitive parse, convert string and delineator to lower case string); delineator); # Return text BEFORE the delineator if(type == EXCL) // Return text ESCL of the delineator lc_str, split_here = strpos(marker)+strlen(parsed_string = substr(split_here); } # Return text AFTER the delineator else { if(split_here = strpos(marker) + strlen(split_here = strpos(marker) ; string, string)); } return
********************************************************************* value = return_between(start, type) ------------------------------------------------------------- DESCRIPTION: Returns a substring of start and string Input string to parse end Defines the end of the sub string
function return_between(string, stop, temp = split_string(start, AFTER, temp, type); }
********************************************************************* string, close_tag) ------------------------------------------------------------- DESCRIPTION: Returns an array of strings that exists repeatedly in string String that contains the tags <a>") close_tag Name of the closing tag (i.e. "</title>") *********************************************************************
function parse_array(beg_tag, beg_tag(.*)string, matching_data[0]; }
********************************************************************* tag, tag The tag that contains the attribute
function get_attribute(tag, cleaned_html = tidy_html(cleaned_html = str_replace("\r", "", cleaned_html = str_replace("\n", "", cleaned_html, strtoupper(
********************************************************************* remove(string, close_tag) ------------------------------------------------------------- DESCRIPTION: Removes all text between close_tag INPUT: open_tag The starting delimitor
function remove(string, close_tag) { # Get array of things that should be removed from the input string string, close_tag); # Remove each occurrence of each array element from string; for(xx<count(xx++) remove_array, "", string; }
********************************************************************* tidy_html(string raw HTML OUTPUT: Returns a string of cleaned-up HTML *********************************************************************
function tidy_html(input_string); config = array( 'uppercase-attributes' => true, 'wrap' => 800); tidy->parseString(config, 'utf8'); cleaned_html = tidy_get_output(cleaned_html = cleaned_html; }
(C) Æliens 20/2/2008
You may not copy or print any of this material without explicit permission of the author or the publisher. In case of other copyright issues, contact the author.