Extract_Body() - Extract the body out of a HTML web page.


PHP Code
 public function Extract_Body($Page, $Default = null, $IncludeTags = null) {
		/*
		Purpose
			Used to extract the body of an HTML page.
		Parameters
			Page (string) = The HTML page we're extracting the body from.
			Default (optional any, null) = The value that is returned if no <body> tags are found in Page.
				Note that this value can be any type you wish.
				If null, the entire page is returned.
				Default: null
			IncludeTags (optional boolean, null) = Used to determine if the enclosing <body> tags are returned.
				If true, the enclosing <body> tags are returned.
				If false, they are stripped from the returned string.
				Default if null: false
				Default: null
		Return (string)
			Returns the body of the specified HTML page (Page).
			See Flags for details about other return options.
		Notes
			This function is very flexible in that it handles opening <body> tags that contain attibutes.
				For example: <body bgcolor="#FFFFFF">
			This function also handles <body> tags in a case-insensitive manner.
				For example: <BODY> or <BoDy> will parse properly.
			This function also avoids the nasty "pcre.backtrack_limit" that is currently an issue when dealing with
				backtracking extressions on files that are longer than this PHP setting, which defaults to 100,000 bytes.
				This issue obviously won't be going away soon.
				If we didn't care about the this limitation because we knew all of our source pages would be
				shorter than 100,000 bytes (or whatever the current PHP installation setting is), then we could have
				used this regular expression "/<body[^>]*>(.*?)<\/body>/is" and avoided the second preg_match() call,
				but speed tests on files a little larger then 100,000 bytes were comparable.
		Version History
			1.0 2009.11.17 Tested by Russ Tanner on PHP 5.2.4 Linux.
		*/

		// Get the body of the page including the <body> tags.
		preg_match('/<body.*\/body>/is', $Page, $Matches);
		// Matches[0] now contains our parsed page (with <body> tags).

		// No <body> tags were found.
		if (strlen($Matches[0]) == 0) {
			// Caller wants entire page returned when <body> tags are missing.
			if (is_null($Default)) return $Page;
			// Return the specified default value.
			return $Default;
		}

		// The caller wants the page returned with the <body> tags included.
		if ($IncludeTags === true) return $Matches[0];

		// Caller does not want the <body> tags included. Strip them off...
		// Get the length of the matching starting <body*> tag.
		preg_match('/<body[^>]*>/is', $Matches[0], $BodyMatch);
		$BodyStartTagLength = strlen($BodyMatch[0]);
		// Get the position of the ending </body> tag, case-insensitive.
		$BodyEndTagPosition = stripos($Matches[0], '</body>');
		// Parse and return the resulting substring between the <body> tags.
		return substr($Matches[0], $BodyStartTagLength, $BodyEndTagPosition - $BodyStartTagLength);

	} 


The Captian
Today they call you "crazy". Tomorrow they call you "ahead of your time."
Global Skywatch Learn about Chemtrails - You're breathing them now!
OnlyTheBestHerbs.com World-class supplements
Mercury Talk Why you are sick.
OneUp Domains Domains, Hosting, Email
1-800-358-4278 (U.S. & Canada)