获取某网站某地址的meta,get_meta_tags()

本文介绍了一个使用PHP解析HTML元标签的实际案例,通过分析具体的HTML代码来获取网页的描述、关键词等元信息,并展示了如何利用PHP脚本来实现这一功能。

版权声明:原创作品,允许转载,转载时请务必以超链接形式标明文章原始出版、作者信息和本声明。否则将追究法律责任。http://blog.youkuaiyun.com/mayongzhan - 马永占,myz,mayongzhan


---------------------------------------------------- get_meta_tags()
解析目标:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta content="all" name="robots" />
<meta name="keywords" content="关键字,关键字2,charset=utf-8" />
<meta name="author" content="mayongzhan" />
<meta name="copyright" content="非本站同意,禁止转载,agree to copy when i allow you" />
<meta name="description" content="描述" />
<title>无标题文档</title>
</head>
<body>
test
</body>
</html>
程序:
<?php
/**
* @name test.php
* @date Sun Jan 19 10:15:26 CST 2008
* @copyright 马永占(MyZ)
* @author 马永占(MyZ)
* @link http://blog.youkuaiyun.com/mayongzhan/
*/
$tags=get_meta_tags('http://127.0.0.1/test.html');
print_r($tags) ;
?>
结果
Array ( [robots] => all [keywords] => 关键字,关键字2,charset=utf-8 [author] => mayongzhan [copyright] => 非本站同意,禁止转载,agree to copy when i allow you [description] => 描述 )
if (!function_exists('get_article_for_category')) { function get_article_for_category( $category, $posts_per_page = 5, $tag = '' ,// 新增参数:标签(tag) $include_meta = true, $include_acf = true, $orderby = 'menu_order', $order = 'DESC' ) { // 限制只允许的排序字段 $allowed_orderby = array('date', 'menu_order'); if (!in_array($orderby, $allowed_orderby)) { $orderby = 'date'; // 默认回退到按时间排序 } // 设置基础查询参数 $args = array( 'posts_per_page' => $posts_per_page, 'orderby' => $orderby, 'order' => $order, ); // 添加分类查询条件 if (is_numeric($category)) { $args['tax_query'][] = array( 'taxonomy' => 'category', 'field' => 'term_id', 'terms' => $category, ); } else { $args['tax_query'][] = array( 'taxonomy' => 'category', 'field' => 'slug', 'terms' => $category, ); } if (!empty($tag)) { $args['tax_query']['relation'] = 'AND'; if (is_numeric($tag)) { $args['tax_query'][] = array( 'taxonomy' => 'post_tag', 'field' => 'term_id', 'terms' => $tag, ); } else { $args['tax_query'][] = array( 'taxonomy' => 'post_tag', 'field' => 'slug', 'terms' => $tag, ); } } if ($orderby === 'menu_order') { $args['orderby'] = 'menu_order'; } $query = new WP_Query($args); if ($query->have_posts()) { $posts = array(); while ($query->have_posts()) { $query->the_post(); $post_id = get_the_ID(); $post_data = array( 'id' => $post_id, 'title' => get_the_title(), 'permalink' => get_the_permalink(), 'excerpt' => get_the_excerpt(), 'content' => get_the_content(), 'thumbnail' => has_post_thumbnail() ? get_the_post_thumbnail_url() : '', 'date' => get_the_date(), 'author' => get_the_author(), 'modified_date' => get_the_modified_date(), 'type' => get_post_type(), 'status' => get_post_status(), 'comment_count' => get_comments_number(), ); // 获取所有自定义字段(post meta) if ($include_meta) { $meta = get_post_meta($post_id); $post_data['meta'] = $meta; } // 如果使用了 ACF,获取 ACF 字段 if ($include_acf && function_exists('get_fields')) { $acf_fields = get_fields(); if ($acf_fields) { $post_data['acf'] = $acf_fields; } } $posts[] = $post_data; } wp_reset_postdata(); return $posts; } return false; } }
最新发布
07-22
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值