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