我需要提取Wordpress文章内容链接中包含的所有具有指定域名的href。
域名"stackoverflow.com"的示例:
- https://stackoverflow.com/question/123456789/lorem-ipsum
- https://stackoverflow.com/question/123456789/dolor-sit
- https://stackoverflow.com/question/123456789/lorem-amet
有没有办法通过MySQL查询或PHP脚本来做到这一点?
提前感谢
也许我找到了一个解决方案:
- 创建一个名为"链接"的Wordpress页面
-
在使用的主题文件夹中创建一个名为"pagelinks.PHP"的PHP文件,其中包含tis代码:
function getUrls($string) { $regex = '/https?':'/'/[^'" ]+/i'; preg_match_all($regex, $string, $matches); return ($matches[0]); } $the_query = new WP_Query('posts_per_page=-1'); while ($the_query->have_posts()) { $the_query->the_post(); $_post_id = get_the_id(); $_post_content = get_post_field( 'post_content', $_post_id); $urls = getUrls($_post_content); foreach($urls as $url) { if (substr($url, 0, 24) == "http://stackoverflow.com") echo $url . '<br />'; } } wp_reset_postdata();
然后在bowser中调用Wordpress页面。通过这种方式,所有在帖子中找到的域名为"stackoverflow.com"的URL都将被打印出来。