我最近刚从博客网站将所有内容导入到wordpress中,我需要整理一些东西。
我正在single.php中工作,我想从<a><img src=""/></a>
获取每个the_content();
。我的php充其量是一点伪劣。
我知道这是发布该帖子的第一张图片,但是我需要类似的内容,它可以将the_content();
中的所有图片(不是特色图片)从我那里获取。
function catch_that_image() {
global $post, $posts;
$first_img = '';
ob_start();
ob_end_clean();
$output = preg_match_all('/<img.+src=[\'"]([^\'"]+)[\'"].*>/i', $post->post_content, $matches);
$first_img = $matches[1][0];
if(empty($first_img)) {
$first_img = "/path/to/default.png";
}
return $first_img;
}
参考方案
看一下PHP本机DOMDocument对象。
您将获取内容并通过loadHTML()
将其加载到DOMDocument中。然后,您可以使用getElementsByTagName()
获取所有图像。
http://www.php.net/manual/en/class.domdocument.php
$document = new DOMDocument();
$document->loadHTML($post->post_content);
$images = $document->getElementsByTagName('img');
无法访问heredoc中的数组 - phpThis question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not…
使用Ajax呈现html表 - php我想知道如何实现以下项目其实我有一个php代码,可以渲染一张桌子<table id = "oldata" class ="table table-bordered"> <thead> <tr class ="success"> <th class="…
哪个更好的做法?从Jquery响应获取HTML - php这只是一个问题,以了解人们如何以及如何做到这一点,但是假设用户向列表中添加了一些内容,完成后,它将运行下面的ajax并更新.user-stream-list$.ajax({ url: "user-stream-list.php", success: function(data){ $(".user-stream-list…
单击按钮后显示列表项行 - php嗨,我尝试开发自定义列表项。加载页面时,列表项中仅应显示<?php echo $row['title']; ?>。用户单击<button id="show" class="btn btn-xs btn-info">Details</button>后,应该显示说明和…
如何使用jquery获取文本字段的val? - php码:<script> $(document).ready(function(){ $(".comments").keyup(function(){ id = this.id; comment = $("#com"+id).val(); alert(comment); }); }); </script&…