在WordPress中获取内容中的图像URL?

时间:2014-09-24 作者:PHP Learner

我想得到image_url 从内容。实际上,我已经完成了以下代码来获取内容:

$content=$query_val->post_content;
我收到以下订单:
"content":"Lorem Ipsum-has been the industry\'s standard dummy text,Lorem Ipsum has been the industry\'s standard dummy text ever since the 1500s,Lorem Ipsum has been the industry\'s standard dummy text ever since the 1500s,Lorem Ipsum has been the industry\'s standard dummy text ever since the 1500s,Lorem Ipsum has been the industry\'s standard dummy text ever since the 1500s..."

当我添加strip_tags() 例如:

 $content=strip_tags($query_val->post_content);
它将删除所有标记,因此我无法获取位于 标签

现在我想在内容中获取图像url,如:


"content":"Lorem Ipsum-has been the industry\'s standard dummy text,Lorem Ipsum has been the industry\'s standard dummy text ever since the 1500s,Lorem Ipsum has been the industry\'s standard dummy text ever since the 1500s,Lorem Ipsum has been the industry\'s standard dummy text ever since the 1500s,Lorem Ipsum has been the industry\'s standard dummy text ever since the 1500s...image_url:"imageurl"

<小时/>

1 个回复
最合适的回答,由SO网友:Nicolai Grossherr 整理而成

听起来您需要对帖子内容执行一些正则表达式。可以这样做,例如:

// get the post object
$post = get_post( get_the_ID() );
// we need just the content
$content = $post->post_content;
// we need a expression to match things
$regex = \'/src="([^"]*)"/\';
// we want all matches
preg_match_all( $regex, $content, $matches );
// reversing the matches array
$matches = array_reverse($matches);
echo \'<pre>\';
// we\'ve reversed the array, so index 0 returns the result
print_r($matches[0]);
echo \'</pre>\';

结束

相关推荐

如何在保存POST、产品并显示“POST SAVED”消息后重定向到edit.php页面

我正在尝试重定向所有帖子页面,一旦帖子或产品被保存。它正在重定向到该页面,但没有显示任何消息。让我知道如何在管理端注册特定页面的消息。add_filter(\'wp_insert_post_data\', \'ccl\', 99); function ccl($data) { if ($data[\'post_type\'] !== \'revision\' && $data[\'post_status\'] == \'publish\') {