2012-09-30 18 views
0

保存HTML標籤變量我有功能<?php the_content(); ?> wihch生成html標籤:如何使用正則表達式

<p> 
<a href="http://127.0.0.1/www/www1/wordpress/wordpressEng/wp-content/uploads/2012/09/page1-img2.jpg"> 
    <img class="alignnone size-full wp-image-82" title="page1-img2" src="http://127.0.0.1/www/www1/wordpress/wordpressEng/wp-content/uploads/2012/09/page1-img2.jpg" alt="" width="219" height="124" /> 
</a> 
</p> 

我嘗試提取<img.**./>但我不sucsses。

財產以後這樣的:

//$String = 
<p> 
<a href="http://127.0.0.1/www/www1/wordpress/wordpressEng/wp-content/uploads/2012/09/page1-img2.jpg"> 
    <img class="alignnone size-full wp-image-82" title="page1-img2" src="http://127.0.0.1/www/www1/wordpress/wordpressEng/wp-content/uploads/2012/09/page1-img2.jpg" alt="" width="219" height="124" /> 
</a> 
</p> 

$String = "the_content()"; 

$pattern = '/^<img/.*/$/>/';//Take string wich start with "<img" and all between and End in "/>" 
preg_match($pattern, substr($subject,3), $StrImg, PREG_OFFSET_CAPTURE); 
echo $StrImg[0]; 
output 
//<img class="alignnone size-full wp-image-82" title="page1-img2" src="http://127.0.0.1/www/www1/wordpress/wordpressEng/wp-content/uploads/2012/09/page1-img2.jpg" alt="" width="219" height="124" /> 

如何設置可變$StrImg完整的img標籤?

非常感謝。

+0

啓用error_reporting。然後刪除多餘的正斜槓,這是您的分隔符。還有更簡單的方法從HTML中提取片段; DOM前端的效率較低,但如果這種情況不受正則表達式的影響,通常更可取。 – mario

回答

3

所以你想獲得你的所有圖像的src屬性後。有更簡單的方法:

$images = get_posts(array(
      'post_parent' => $post->ID, 
      'post_type' => 'attachment', 
      'numberposts' => -1, 
      'post_mime_type' => 'image' 
     )) 

     foreach($images as $image) { 
      $image_url[] = wp_get_attachment_image_src($image->ID, full); 

     } 
+0

非常感謝...... – yossi