Php matches the content of a specified tag. php matches the content of a specified p. when writing a collection program in php, the captured webpage data sometimes requires only a small segment of tag content, how can we extract it from html code? here we provide a function example to implement php matching all the content in any html tag:
/*** Html tag content matching any id **/function getWebTag ($ tag_id, $ tag = 'p', $ 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); // Obtain all p prefixes prefix preg_match_all ('/<\/'. $ tag. '/I', $ data, $ suf_matches, PREG_OFFSET_CAPTURE); // get all p suffixes $ hit = strpos ($ data, $ tag_id ); if ($ hit =-1) return false; // $ ps = array () is not hit (); // merge all p foreach ($ pre_matches [0] as $ index => $ pre_p) {$ ps [(int) $ pre_p [1] = 'p '; $ ps [(int) $ suf_matches [0] [$ index] [1] = 's';} // sort p by $ sort = array_keys ($ ps ); asort ($ sort); $ count = count ($ pre_matches [0]); foreach ($ pre_matches [0] as $ index => $ pre_p ){//'; Break ;}return $ hitDivString ;}
Call example
$ Html = file_get_contents ('http: // www.baidu.com ');
$ PContent = getWebTag ('Id = "content" ', 'P', $ html );