note 76297 deleted from function.preg-replace by cmb
| From: | cmb@php.net | Date: | Sun, 29 Sep 2019 07:27:11 +0000 |
| Subject: | note 76297 deleted from function.preg-replace by cmb | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-212395@lists.php.net to get a copy of this message | ||
Note Submitter: mtsoft at mt-soft dot com dot ar
----
This function takes a URL and returns a plain-text version of the page. It uses cURL to retrieve the
page and a combination of regular expressions to strip all unwanted whitespace. This function will
even strip the text from STYLE and SCRIPT tags, which are ignored by PHP functions such as
strip_tags (they strip only the tags, leaving the text in the middle intact).
Regular expressions were split in 2 stages, to avoid deleting single carriage returns (also matched
by \s) but still delete all blank lines and multiple linefeeds or spaces, trimming operations took
place in 2 stages.
<?php
function webpage2txt($url)
{
$user_agent = âMozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0)â;
$ch = curl_init(); // initialize curl handle
curl_setopt($ch, CURLOPT_URL, $url); // set url to post to
curl_setopt($ch, CURLOPT_FAILONERROR, 1); // Fail on errors
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); // allow redirects
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1); // return into a variable
curl_setopt($ch, CURLOPT_PORT, 80); //Set the port number
curl_setopt($ch, CURLOPT_TIMEOUT, 15); // times out after 15s
curl_setopt($ch, CURLOPT_USERAGENT, $user_agent);
$document = curl_exec($ch);
$search = array(â@<script[^>]*?>.*?</script>@siâ, // Strip out javascript
â@<style[^>]*?>.*?</style>@siUâ, // Strip style tags properly
â@<[\/\!]*?[^<>]*?>@siâ, // Strip out HTML tags
â@<![\s\S]*?â[ \t\n\r]*>@â, // Strip multi-line comments including
CDATA
â/\s{2,}/â,
);
$text = preg_replace($search, â\nâ, html_entity_decode($document));
$pat[0] = â/^\s+/â;
$pat[2] = â/\s+\$/â;
$rep[0] = ââ;
$rep[2] = â â;
$text = preg_replace($pat, $rep, trim($text));
return $text;
}
?>
Potential uses of this function are extracting keywords from a webpage, counting words and things
like that. If you find it useful, drop us a comment and let us know where you used it.