1

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

  <img src=images/imagename.gif alt='descriptive text here'>

そして、私はそれを次の2つの文字列に分割しようとしています(2つの文字列の配列、何であれ、分割されたばかりです)。

  imagename.gif
  descriptive text here

はい、それは実際には&lt;であり、ではないことに注意してください<。文字列の終わりと同じです。

私は正規表現が答えであることを知っていますが、PHPでそれを実現する方法を知るには正規表現が十分ではありません。

4

2 に答える 2

2

より良い使用DOM xpath rather than regex

<?php
$your_string = html_entity_decode("&lt;img src=images/imagename.gif alt='descriptive text here'&gt;");
$dom = new DOMDocument;
$dom->loadHTML($your_string);
$x = new DOMXPath($dom); 

foreach($x->query("//img") as $node) 
{
    echo $node->getAttribute("src");
    echo $node->getAttribute("alt");
}

?>
于 2012-12-09T04:58:12.407 に答える
2

これを試して:

<?php

$s="&lt;img src=images/imagename.gif alt='descriptive text here'&gt;";

preg_match("/^[^\/]+\/([^ ]+)[^']+'([^']+)/", $s, $a);

print_r($a);

出力:

Array
(
    [0] => &lt;img src=images/imagename.gif alt='descriptive text here
    [1] => imagename.gif
    [2] => descriptive text here
)
于 2012-12-09T02:37:43.130 に答える