Hi;
I have a simple problem. I want to match anything between "<td>...</td>" tags.
My string is
<td>

<a class="page" title="myPage" href="/mypage/888"><img class="hotel" src="mypage.png" /></a>

</td>

I have tried several ways but couldn't get "<a class="page" title="myPage" href="/mypage/888"><img class="hotel" src="mypage.png" /></a>".
Thanks.

<?php
$reg = '/<td>(.*)<\/td>/s';
$sub = '<td><a class="page" title="myPage" href="/mypage/888"><img class="hotel" src="mypage.png" /></a></td>';
preg_match($reg,$sub,$matches);
print htmlentities($matches[1]);
?>

Cheers!

Thanks a lot.
how can I get anything between <a>...</a> in this situation?
I try

'/<td><a.*>(.*)<\/a><\/td>/s'

but it doesnt help. returns nothing.

Member Avatar for langsor

You need to take into consideration arbitrary white space (\s*), and any time you have a catch all character (.* or .+) you should consider if it's greedy and going to grab everything to the end of your input string ... to stop that greedy behavior you can follow the character with a (?) ...

Another handy method set for parsing HTML and XML data (both tags and text) is the DOM methods collection ... I dropped in an example of that too, though I don't know your exact scenario so couldn't tailor it to your need.
http://us2.php.net/manual/en/class.domdocument.php

<?php

$input = <<<endline
<td>

<a class="page" title="myPage" href="/mypage/888"><img class="hotel" src="mypage.png" /></a>

</td>
endline;

preg_match( '#<td>\s*<a.*?\>(.*)</a>\s*</td>#si', $input, $output );
print htmlentities( $output[1] );


// using DOM Methods to access HTML or XML node values ...
$doc =  new DOMDocument('1.0');
$doc->loadHTML( $input ); // there are also methods to load whole files
$links = $doc->getElementsByTagName('a');
foreach ( $links as $idx => $link ) {
  if ( $link->getAttribute('title') == 'myPage' ) {
    switch ( $link->firstChild->nodeType ) {
      case 1: // elements
      print $link->firstChild->nodeName;
      break;
      case 3: // text nodes
      print $link->firstChild->nodeValue;
      break;
    }
  }
}
?>
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.