php抓取网页连接函数( 8.查看你的电子邮件是否已读当发送电子邮件时,怎么办?)
优采云 发布时间: 2022-03-29 17:12php抓取网页连接函数(
8.查看你的电子邮件是否已读当发送电子邮件时,怎么办?)
function combine_my_files($array_files, $destination_dir, $dest_file_name){
if(!is_file($destination_dir . $dest_file_name)){ //continue only if file doesn't exist
$content = "";
foreach ($array_files as $file){ //loop through array list
$content .= file_get_contents($file); //read each file
}
//You can use some sort of minifier here
//minify_my_js($content);
$new_file = fopen($destination_dir . $dest_file_name, "w" ); //open file for writing
fwrite($new_file , $content); //write to destination
fclose($new_file);
return ''; //output combined file
}else{
//use stored file
return ''; //output combine file
}
}
而且,用法是这样的:
$files = array(
'http://example/files/sample_js_file_1.js',
'http://example/files/sample_js_file_2.js',
'http://example/files/beautyquote_functions.js',
'http://example/files/crop.js',
'http://example/files/jquery.autosize.min.js',
);
echo combine_my_files($files, 'minified_files/', md5("my_mini_file").".js");
3.检查您的电子邮件是否已被阅读
发送电子邮件时,您会想知道您的消息是否已被阅读。这是一个非常有趣的代码片段,它记录了读取您邮件的 IP 地址以及实际日期和时间。
4.从网页中提取关键词
正如副标题所说:这个代码片段可以让你轻松地从网页中提取 meta关键词。
$meta = get_meta_tags('http://www.emoticode.net/');
$keywords = $meta['keywords'];
// Split keywords
$keywords = explode(',', $keywords );
// Trim them
$keywords = array_map( 'trim', $keywords );
// Remove empty values
$keywords = array_filter( $keywords );
print_r( $keywords );
5.查找页面上的所有链接
使用 DOM,您可以轻松抓取网络上的所有链接。这是一个工作示例:
$html = file_get_contents('http://www.example.com');
$dom = new DOMDocument();
@$dom->loadHTML($html);
// grab all the on the page
$xpath = new DOMXPath($dom);
$hrefs = $xpath->evaluate("/html/body//a");
for ($i = 0; $i < $hrefs->length; $i++) {
$href = $hrefs->item($i);
$url = $href->getAttribute('href');
echo $url.'<br />';
}
6.自动将 URL 转换为可点击的超链接
在 WordPress 中,如果您想自动将字符串中的所有 URL 转换为可点击的超链接,那么使用内置函数 make_clickable() 就可以了。如果您需要在 WordPress 之外执行此操作,可以参考 wp-includes/formatting.php 中该函数的源代码:
function _make_url_clickable_cb($matches) {
$ret = '';
$url = $matches[2];
if ( empty($url) )
return $matches[0];
// removed trailing [.,;:] from URL
if ( in_array(substr($url, -1), array('.', ',', ';', ':')) === true ) {
$ret = substr($url, -1);
$url = substr($url, 0, strlen($url)-1);
}
return $matches[1] . "<a href=\"$url\" rel=\"nofollow\">$url</a>" . $ret;
}
function _make_web_ftp_clickable_cb($matches) {
$ret = '';
$dest = $matches[2];
$dest = 'http://' . $dest;
if ( empty($dest) )
return $matches[0];
// removed trailing [,;:] from URL
if ( in_array(substr($dest, -1), array('.', ',', ';', ':')) === true ) {
$ret = substr($dest, -1);
$dest = substr($dest, 0, strlen($dest)-1);
}
return $matches[1] . "<a href=\"$dest\" rel=\"nofollow\">$dest</a>" . $ret;
}
function _make_email_clickable_cb($matches) {
$email = $matches[2] . '@' . $matches[3];
return $matches[1] . "<a href=\"mailto:$email\">$email</a>";
}
function make_clickable($ret) {
$ret = ' ' . $ret;
// in testing, using arrays here was found to be faster
$ret = preg_replace_callback('#([\s>])([\w]+?://[\w\\x80-\\xff\#$%&~/.\-;:=,?@\[\]+]*)#is', '_make_url_clickable_cb', $ret);
$ret = preg_replace_callback('#([\s>])((www|ftp)\.[\w\\x80-\\xff\#$%&~/.\-;:=,?@\[\]+]*)#is', '_make_web_ftp_clickable_cb', $ret);
$ret = preg_replace_callback('#([\s>])([.0-9a-z_+-]+)@(([0-9a-z-]+\.)+[0-9a-z]{2,})#i', '_make_email_clickable_cb', $ret);
// this one is not in an array because we need it to run last, for cleanup of accidental links within links
$ret = preg_replace("#(]+?>|>))<a [^>]+?>([^>]+?)</a></a>#i", "$1$3</a>", $ret);
$ret = trim($ret);
return $ret;
}
7.在您的服务器上下载并保存远程图像
在构建 网站 时,在远程服务器上下载图像并将其保存在您自己的服务器上很有用,而且它也很容易做到。以下两行代码将为您完成。
$image = file_get_contents('http://www.url.com/image.jpg');
file_put_contents('/images/image.jpg', $image); //Where to save the image
8.检测浏览器语言
如果您的 网站 使用多种语言,则检测浏览器语言并将该语言设置为默认语言会很有用。下面的代码将返回客户端浏览器使用的语言。
function get_client_language($availableLanguages, $default='en'){
if (isset($_SERVER['HTTP_ACCEPT_LANGUAGE'])) {
$langs=explode(',',$_SERVER['HTTP_ACCEPT_LANGUAGE']);
foreach ($langs as $value){
$choice=substr($value,0,2);
if(in_array($choice, $availableLanguages)){
return $choice;
}
}
}
return $default;
}
9.显示 Facebook 关注者数量的全文
如果您的 网站 或博客有 Facebook 页面,那么您可能想要显示您有多少关注者。此代码段可以帮助您获取 Facebook 关注者的数量。不要忘记在第二行添加您的页面 ID。页面 ID 可以在地址中找到。
<p>