1

私はこの文字列を持っています:

$string = '<div class="displayEmbededImage" sourcefile="http://site.com/images/myImage.jpg" style="width:200px;"><p>Some text goes here</p></div>';

sourcefile のコンテンツを選択して次のように返す正規表現が必要です。

$parern = '/<div(.*)sourcefile="([^"]+)"(.*)>(.*)<\/div>/s';
preg_replace($pattern, '<img src="$1">', $string);

それは私がこれまでに持っているものですが、まだ完全には理解していません

4

2 に答える 2

4

html では正規表現を使用しません。試してはいけません。代わりに DOM を使用します。

$d = new DOMdocument();
$d->loadHTML('... your html here ...');
$xp = new DOMXpath($d);
$res = $xp->query('//div[@class="displayEmbededImage"]');
$source = $res->item(0)->getAttribute('sourcefile');
于 2013-03-21T17:50:02.420 に答える
2

変数名にタイプミスがあり、正しいプレースホルダーを使用していません:

echo preg_replace($parern, '<img src="$2">', $string);
于 2013-03-21T17:50:00.793 に答える