Php matches the content of the specified tag

Source: Internet
Author: User
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 );

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.