0

複数の画像(場合によっては1つ、場合によっては2つ、場合によっては3つ)を含むブログエントリがあり、次のようになります。

<a href="http://xxx/" rel="attachment w133>
  <img class="yyy" title="title1" src="http://xxx/title1.jpg" alt="" width="650" height="487" />
</a>
<a href="http://xxx/" rel="attachment w134">
  <img class="yyy" title="title2" src="http://xxx/title2.jpg" alt="" width="650" height="487" />
</a>
<a href="http://xxx/" rel="attachment w135">
  <img class="yyy" title="title3" src="http://xxx/title3.jpg" alt="" width="650" height="487" />
</a>

これに続くいくつかのテキストで。

さて、preg_match_allを使用してその最初の部分を抽出するにはどうすればよいのでしょうか。私は今やPHPプログラミングについていくらか話しますが、preg_match_allを使用したことはありません。

ここでのこのコードは、最後の画像のみを抽出しますが、これでは不十分です。

$output = preg_match_all('/<img.+src=[\'"]([^\'"]+)[\'"].*>/i', $post_content, $matches);

可能であれば、誰かが私にそれを達成する方法のヒントを与えることができれば素晴らしいでしょう。本当にありがとう!

4

2 に答える 2

3
$post_content='<a href="http://xxx/" rel="attachment w133>
  <img class="yyy" title="title1" src="http://xxx/title1.jpg" alt="" width="650" height="487" />
</a>
<a href="http://xxx/" rel="attachment w134">
  <img class="yyy" title="title2" src="http://xxx/title2.jpg" alt="" width="650" height="487" />
</a>
<a href="http://xxx/" rel="attachment w135">
  <img class="yyy" title="title3" src="http://xxx/title3.jpg" alt="" width="650" height="487" />
</a>
';

preg_match_all('/<a\s[^>]*href=([\"\']??)([^\" >]*?)\\1[^>]*>(.*)<\/a>/siU', $post_content, $matches);
//print_r ($matches);//$matches - array which contains all your images
print $matches[0][0]; //first link with image
print $matches[0][1]; //second link with image
print $matches[0][2]; //third link with image

出力:

<a href="http://xxx/" rel="attachment w133&gt;
  &lt;img class=" yyy"="" title="title1" src="http://xxx/title1.jpg" alt="" width="650" height="487">
</a>
<a href="http://xxx/" rel="attachment w134">
  <img class="yyy" title="title2" src="http://xxx/title2.jpg" alt="" width="650" height="487">
</a>
<a href="http://xxx/" rel="attachment w135">
  <img class="yyy" title="title3" src="http://xxx/title3.jpg" alt="" width="650" height="487">
</a>
于 2012-07-12T14:34:57.467 に答える
0

試す:

preg_match_all('/<img.+src=[\'"]([^\'"]+)[\'"][^\/>]*>/Ui', $post_content, $matches);
print_r($matches);
于 2012-07-12T14:35:41.863 に答える