php - html dom parser to extract href from span sibiling -


here html file contains date , link in <span> tag within table. can me find link of particular date. view link of particular date

<table> <tbody> <tr class="c0"> <td class="c11"> <td class="c8"> <ul class="c2 lst-kix_h6z8amo254ry-0 start"> <li class="c1"> <span>1st apr 2014 - </span> <span class="c6"><a class="c4" href="/link.html">view</a> </span> </li> </ul> </td> </tr> </td> </table> 

i want retrieve link particular date

my code this

include('simple_html_dom.php');     $html = file_get_html('link.html'); //store links in array foreach($html->find('span') $value) {     //echo $value->plaintext . '<br />';     $date = $value->plaintext;      if (strpos($date,$compare_text)) {          //$linkeachday = $value->find('span[class=c1]')->href;         //$day_url[] = $value->href;         //$day_url = array("text" => $value->plaintext);         $day_url = array("text" => $date, "link" =>$linkeachday);         //echo $value->next_sibling (a);     } } 

or

$spans = $html->find('table',0)->find('li')->find('span'); echo $spans;  $num = null;  foreach($spans $span){      if($span->plaintext == $compare_text){         $next_span = $span->next_sibling();         $num = $next_span->plaintext;          echo($num);             break;       }  }  echo($num); 

you on right path last example...

i modified bit following gets spans, test if have searched text, , if so, displays content of next sibling if there (check in code comments):

$input =  <<<_data_     <table>         <tbody>             <tr class="c0">                 <td class="c11">                     <td class="c8">                         <ul class="c2 lst-kix_h6z8amo254ry-0 start">                             <li class="c1">                                 <span>1st apr 2013 - </span>                                 <span>1st apr 2014 - </span>                                 <span class="c6">                                     <a class="c4" href="/link.html">view</a>                                 </span>                                 <span>1st apr 2015 - </span>                             </li>                         </ul>                     </td>                 </td>             </tr>         </tbody>     </table> _data_;  // create dom object $html = new simple_html_dom(); // load html string $html->load($input);  // searched value $searchdate = '1st apr 2014';  // find spans direct childs of li, descendent of table $spans = $html->find('table li > span');  // loop through spans foreach ($spans $span) {     // if span starts searched text && has following sibling     if ( strpos($span->plaintext, $searchdate) === 0 && $sibling = $span->next_sibling()) {         // then, print it's text content         echo $sibling->plaintext;    // or ->innertext raw content         // , stop (if 1 result needed)         break;     } } 

output

view 

for string comparison, may (for best) use regex...

so in code above, add build pattern:

$pattern = sprintf('~^\s*%s~i', preg_quote($searchdate, '~')); 

and use preg_match test match:

if ( preg_match($pattern, $span->plaintext) && $sibling = $span->next_sibling()) { 

Comments

Popular posts from this blog

javascript - jquery or ashx not working -

opencv - DataType<cv::detail::deriv_type>::depth what is it used for -

python 3.x - Mapping specific letters onto a list of words -