1

ID が特定の文字列で始まり、何かで終わる div からコンテンツを抽出したい。例:

私はdivを持っています

<div id="content_message_56384">
  My first post.
</div>

私のコード:

$regex = '#\<div id="content_message_[*]"\>(.+?)\<\/div\>#s';
preg_match($regex, $intro, $matches);
echo $match = $matches[0];

しかし、私は何の結果も得ていません。助けてください。

4

2 に答える 2

3

コードは次のようになります。

$string = "<div id=\"content_message_56384\">
  My first post.
</div>";
$regex = '/<div id="content_message_(?:\d+)">(.+?)<\/div>/s';
preg_match($regex, $string, $matches);
print($maches[1]);

(?:\d+)の中にあるものだけが必要なため、ここでは非キャプチャ式を使用していることに注意してください<div>

于 2013-06-29T11:41:35.577 に答える
0
$intro='<div id="content_message_56384">
  My first post.
</div><div id="content_message_5577577IIRRIR">
  My second possssss.
</div>';
$regex = '#\<div id="content_message_+.+"\>(.+?)\<\/div\>#s';
preg_match($regex, $intro, $matches);
print_r($matches[0]);
于 2013-06-29T11:38:43.110 に答える