PHP matches the contents of the specified tag

Source: Internet
Author: User

PHP matches the specified div content, when using PHP to write a collection program, crawling to the Web page data sometimes we only need a small piece of the content of the tag, how can we extract the HTML code, here is a function example, to implement PHP matching any HTML tags in all the content:

/***  matches HTML tag contents of any ID * */function getwebtag ($tag _id, $tag = ' div ', $data =false) {               $charset _pos = stripos ($data, ' CharSet ');         if ($charset _pos)  {             if (Stripos ($data, ' utf-8 ', $charset _pos))  {                  $data  = iconv (' utf-8 ', ' utf-8 ', $data);             }else if (Stripos ($data, ' gb2312 ', $charset _pos))  {                  $data  = iconv (' gb2312 ', ' utf-8 ', $data);             }else if (Stripos ($data, ' GBK ', $charset _pos))  {                  $data  = iconv (' GBK ', ' utf-8 ', $data);             }        }                preg_match_all ('/< '. $ Tag. ' /I ', $data, $pre _matches,preg_offset_capture);     //get all div prefixes          preg_match_all ('/<\/'. $tag. ' /I ', $data, $suf _matches,preg_offset_capture);  //get all div suffixes         $ Hit = strpos ($data, $tag _id);         if ($hit  == -1)  return false;    //misses          $divs  = array ();     //merges all Div        foreach ( $pre _matches[0] as  $index = $pre _div) {             $divs [(int) $pre _div[1]] =  ' P ';              $divs [(int) $suf _matches[0][$index][1]] =  ' s ';            }                //to sort the div         $ Sort = array_keys ($divs);         asort ($sort);                 $count  = count ($pre _ Matches[0]);         foreach ($pre _matches[0] as  $index =>$ Pre_div) {            //<div  $hit  < div+1     when Div is hit             if ($pre _matches[0][$index][1] <  $hit)  &&  ($hit  <  $pre _matches[0][$index +1][1]) {                  $deeper  =  0;                // Pop-up hit Div before div                 while (Array_shift ($sort)  !=  $pre _matches[0][$index][1] &&  ($count-))   continue;                // The remaining div is matched, and if the next is prefixed, a layer down, $deeper plus 1,                 //Otherwise back one layer, $deeper minus 1, $deeper 0 hit match, calculate div length                  foreach ($sort  as  $key) {           &nbsP;         if ($divs [$key] ==  ' P ')   $deeper ++;                      else if ($deeper  == 0)  {                          $length  =  $key-$ pre_matches[0][$index][1];                         break;                     }else {                           $deeper--;                     }                 }                $ Hitdivstring = substr ($data, $pre _matches[0][$index][1], $length). ' </'. $tag. ' > ';                break;             }         }        return  $hitDivString;}

Invoke Example

$html =file_get_contents (' http://www.baidu.com ');

$divContent =getwebtag (' id= ' content ', ' div ', $html);


PHP matches the contents of the specified tag

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.