2

Suppose I have a text with a bunch (0 or more) of IMG and A and maybe other HTML tags like this:

hello world hello world <a href='ads'>hello</a> bla bla foo bar <img src='' />

I wanna to match in a regular expression for PHP any A and IMG tag. The A tags should include the TAG content in the match. The other tags other that A and IMG could be discarded for now.

So the result should be:

//match 1
<a href='ads'>hello</a>
//match 2
<img src='' />

Is there maybe a ready solution. Should I use REGEX ?

Claudio Ferraro
  • 4,551
  • 6
  • 43
  • 78
  • *Should I use REGEX ?* I believe that has been answered on this site before. – alex Aug 16 '13 at 05:24
  • yes. I don't know to which case you refer to, but my case is a little bit more complicated. Not all the HTML tags and mixed HTML tags including sometimes some content. – Claudio Ferraro Aug 16 '13 at 05:28

4 Answers4

2

Use DOMDocument. This particular example requires >= 5.3.6:

$content = <<<EOM
hello world hello world <a href='ads'>hello</a> bla bla foo bar <img src='' />
EOM;

$doc = new DOMDocument;
$doc->loadHTML($content);
$xp = new DOMXPath($doc);

foreach ($xp->query('//a | //img') as $node) {
        echo $doc->saveHTML($node);
}

Output:

<a href="ads">hello</a><img src="">
Ja͢ck
  • 170,779
  • 38
  • 263
  • 309
0

Use a DOM parser like this one http://simplehtmldom.sourceforge.net/manual.htm

Finding tags with this is very easy:

// Create DOM from URL or file
$html = file_get_html('http://www.google.com/');

// Find all images
foreach($html->find('img') as $element)
       echo $element->src . '<br>';

// Find all links
foreach($html->find('a') as $element)
       echo $element->href . '<br>'; 
slash197
  • 9,028
  • 6
  • 41
  • 70
0

use DOM:

$dom = new DOMDocument();
$dom->loadHTML("hello world hello world <a href='ads'>hello</a> bla bla foo bar <img src='' />");
$xpath = new DOMXPath($dom);
$nodes = $xpath->query('//a | //img');
foreach($nodes as $node){
    if($node->tagName=='a'){
        //links
    } else if($node->tagName=='img'){
       //images
    }   
}
Im ieee
  • 469
  • 3
  • 17
0

This will shows all IMG tags in one group array and the tags as another group array.

$match = array();

echo $str = "hello world hello world <a href='ads'>hello<img src='test1' /></a> bla bla       foo bar <img src='' /> fssf <img src='test2' />";

//IMG Match

preg_match_all("/<img[^>]+\>/i", $str, $match);
echo "IMG Match";
if (is_array($match[0])) {
   foreach ($match[0] as $key => $val) {
       echo "<br/>" . $val;
   }
}
var_dump($match);

$match = array();
//A Match
preg_match_all("#<a[^>]*>.*?</a>#i", $str, $match);
echo "<A> Match <br/>";
if (is_array($match[0])) {
   foreach ($match[0] as $key => $val) {
       echo "<br/>" . $val;
   }
}
var_dump($match);
Ajeesh
  • 277
  • 2
  • 8