Bandwith saving and fast response times with etag

Recently i found a Post about Etags and thought this will be usefull for bandwith saving and faster response times in my scripts.

Just use this nice function:

function deliver_content($content)
{
	$etag = '"' . md5($content) . '"';
	if (!isset($_SERVER['HTTP_IF_NONE_MATCH'])) {
		$_SERVER['HTTP_IF_NONE_MATCH'] = '';
	}
	header('Cache-Control: must-revalidate, proxy-revalidate');
	header('ETag: ' . $etag);
	if ($_SERVER['HTTP_IF_NONE_MATCH'] == $etag) {
		header('HTTP/1.0 304 Not Modified');
	} else {
		echo $content;
	}
}

So all your content of this site will be cached in the browser cache, until the etag differs. I just used an md5 of the content, but you could use also other strings.

Find links in a website without REGEX parsing

Everybody has needed it before... Website parsing for specific tags.
Naturally everbody try to solve this via REGEX. But how can we do that smarter? The solution is the domDocument class in PHP. This can be used to parse all Elements from HTML Code. In this Example i have used links.

Lets explain this via code:

 
// a site to search for links
$link = 'http://www.php.net';
// get the html code via file_get
$html = file_get_contents($link);
$ret = array();
// create a new dom object
$dom = new domDocument;
 
// load the HTML in the dom class, suppress parsing errors with @
@$dom->loadHTML($html);
 
// get all anchors from the html code
$anchor = $dom->getElementsByTagName('a');
 
/*** get the url from the links ***/
foreach ($anchor as $tag)
{
	// in $tag->getAttribute('href') is the url from the anchor
	// $tag->childNodes->item(0)->nodeValue is the value inside <a>value</a>
	echo $tag->childNodes->item(0)->nodeValue
	echo  ' -> '. $tag->getAttribute('href') . PHP_EOL;
}