如何通过页面中的正则表达式获取3张图片: 我有一个代码:
<?php
$post_id = 29;
$post_content = get_post($post_id);
$content = $post_content->post_content;
$regex = '/<img.+src=[\'"]([^\'"]+)[\'"].*>/i';
$output = preg_match_all($regex, $content, $images);
$content = preg_replace($regex, '', $content);
?>
<div class='gallery_item'>
<div class="readmore"><a href="/airports">See more projects</a></div>
<?php
foreach ( $images[0] as $image ) {
echo '<div class="item_thumbnail col-sm-3 col-md-4"><a href="/airports">' . $image . '</a></div>';
}
?>
<div class="project-title"><h4>Airports</h4></div>
</div>
我不知道如何添加条件以从页面获取3张图片 能给我看看么 ? 我是新手:( 谢谢!
答案 0 :(得分:0)
如果您只想显示3张图片:
<?php
$post_id = 29;
$post_content = get_post($post_id);
$content = $post_content->post_content;
$regex = '/<img.+src=[\'"]([^\'"]+)[\'"].*>/i';
$output = preg_match_all($regex, $content, $images);
$content = preg_replace($regex, '', $content);
?>
<div class='gallery_item'>
<div class="readmore"><a href="/airports">See more projects</a></div>
<?php
$nb_to_display = 3;
for ($idx = 0; $idx < $nb_to_display; $idx++) {
echo '<div class="item_thumbnail col-sm-3 col-md-4"><a href="/airports">' . $images[0][$idx] . '</a></div>';
}
?>
<div class="project-title"><h4>Airports</h4></div>
</div>