編集:今、私はよく知っています
この種の問題を解決するために regexp を使用することは悪い考えであり、メンテナンスや信頼性の低いコードにつながる可能性があります。HTML パーサーを使用することをお勧めします。
正規表現を使用したソリューション
その場合、プロセスを 2 つの部分に分割することをお勧めします。
- すべての img タグを取得する
- メタデータを抽出する
ドキュメントが xHTML に厳密ではないため、XML パーサーを使用できないと仮定します。この Web ページのソース コードを使用した EG :
/* preg_match_all match the regexp in all the $html string and output everything as
an array in $result. "i" option is used to make it case insensitive */
preg_match_all('/<img[^>]+>/i',$html, $result);
print_r($result);
Array
(
[0] => Array
(
[0] => <img src="/Content/Img/stackoverflow-logo-250.png" width="250" height="70" alt="logo link to homepage" />
[1] => <img class="vote-up" src="/content/img/vote-arrow-up.png" alt="vote up" title="This was helpful (click again to undo)" />
[2] => <img class="vote-down" src="/content/img/vote-arrow-down.png" alt="vote down" title="This was not helpful (click again to undo)" />
[3] => <img src="http://www.gravatar.com/avatar/df299babc56f0a79678e567e87a09c31?s=32&d=identicon&r=PG" height=32 width=32 alt="gravatar image" />
[4] => <img class="vote-up" src="/content/img/vote-arrow-up.png" alt="vote up" title="This was helpful (click again to undo)" />
[...]
)
)
次に、ループを使用してすべての img タグ属性を取得します。
$img = array();
foreach( $result as $img_tag)
{
preg_match_all('/(alt|title|src)=("[^"]*")/i',$img_tag, $img[$img_tag]);
}
print_r($img);
Array
(
[<img src="/Content/Img/stackoverflow-logo-250.png" width="250" height="70" alt="logo link to homepage" />] => Array
(
[0] => Array
(
[0] => src="/Content/Img/stackoverflow-logo-250.png"
[1] => alt="logo link to homepage"
)
[1] => Array
(
[0] => src
[1] => alt
)
[2] => Array
(
[0] => "/Content/Img/stackoverflow-logo-250.png"
[1] => "logo link to homepage"
)
)
[<img class="vote-up" src="/content/img/vote-arrow-up.png" alt="vote up" title="This was helpful (click again to undo)" />] => Array
(
[0] => Array
(
[0] => src="/content/img/vote-arrow-up.png"
[1] => alt="vote up"
[2] => title="This was helpful (click again to undo)"
)
[1] => Array
(
[0] => src
[1] => alt
[2] => title
)
[2] => Array
(
[0] => "/content/img/vote-arrow-up.png"
[1] => "vote up"
[2] => "This was helpful (click again to undo)"
)
)
[<img class="vote-down" src="/content/img/vote-arrow-down.png" alt="vote down" title="This was not helpful (click again to undo)" />] => Array
(
[0] => Array
(
[0] => src="/content/img/vote-arrow-down.png"
[1] => alt="vote down"
[2] => title="This was not helpful (click again to undo)"
)
[1] => Array
(
[0] => src
[1] => alt
[2] => title
)
[2] => Array
(
[0] => "/content/img/vote-arrow-down.png"
[1] => "vote down"
[2] => "This was not helpful (click again to undo)"
)
)
[<img src="http://www.gravatar.com/avatar/df299babc56f0a79678e567e87a09c31?s=32&d=identicon&r=PG" height=32 width=32 alt="gravatar image" />] => Array
(
[0] => Array
(
[0] => src="http://www.gravatar.com/avatar/df299babc56f0a79678e567e87a09c31?s=32&d=identicon&r=PG"
[1] => alt="gravatar image"
)
[1] => Array
(
[0] => src
[1] => alt
)
[2] => Array
(
[0] => "http://www.gravatar.com/avatar/df299babc56f0a79678e567e87a09c31?s=32&d=identicon&r=PG"
[1] => "gravatar image"
)
)
[..]
)
)
正規表現は CPU を集中的に使用するため、このページをキャッシュすることをお勧めします。キャッシュ システムがない場合は、ob_startを使用してテキスト ファイルから読み込み/保存することで、独自のシステムを微調整できます。
これはどのように機能しますか?
まず、パターンに一致するすべての文字列を取得し、それを 3 番目のパラメーターに出力する関数であるpreg_ match_ allを使用します。
正規表現:
<img[^>]+>
すべての html Web ページに適用します。" " で始まり、">" 以外の文字を含み、 > で終わるすべての文字列<img
として読み取ることができます。
(alt|title|src)=("[^"]*")
各 img タグに連続して適用します。これは、「alt」、「title」、または「src」で始まり、「=」、「"」、「"」ではなく「"」で終わるすべての文字列として読み取ることができます。 () の間の部分文字列を分離します。
最後に、正規表現を扱いたいときはいつでも、すぐにテストできる優れたツールがあると便利です。このオンライン正規表現テスターを確認してください。
編集:最初のコメントへの回答。
一重引用符を使用している (願わくば少数の) 人々について考えていなかったのは事実です。
' だけを使用する場合は、すべての " を ' に置き換えてください。
両方混ぜれば。まず、自分自身を平手打ちする必要があります :-) 代わりに ("|') を使用するか、" と [^ø] を使用して [^"] を置き換えます。