PHP Cut page div content Implementation code sharing _php instance

Source: Internet
Author: User
Highlight:
1, the use of PHP can also achieve the page div cutting processing. Here, we hope that readers can provide a more perfect solution.
2, cutting processing method has been encapsulated into a method, can be directly referenced.
3, by the way plus the interception of the tag cloud. Getwebdiv (' id= ' taglist ', ' http://www.jb51.net/tag/');
Copy Code code as follows:

<?php
Header ("content-type:text/html; Charset=utf-8 ");
function Getwebdiv ($div _id, $url =false, $data =false) {
if ($url!== false) {
$data = file_get_contents ($url);
}
$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 ('/<div/i ', $data, $pre _matches,preg_offset_capture); Get all div prefixes
Preg_match_all ('/<\/div/i ', $data, $suf _matches,preg_offset_capture); Get all div suffixes
$hit = Strpos ($data, $div _id);
if ($hit = = 1) return false; Not hit
$divs = Array (); Merge 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 a div
$sort = Array_keys ($divs);
Asort ($sort);
$count = count ($pre _matches[0]);
foreach ($pre _matches[0] as $index => $pre _div) {
<div $hit <div+1 Div is hit
if ($pre _matches[0][$index][1] < $hit) && ($hit < $pre _matches[0][$index +1][1)) {
$deeper = 0;
Popup div before the hit Div
while (Array_shift ($sort)!= $pre _matches[0][$index][1] && ($count-)) continue;
Match the remaining Div, and if the next one is prefixed, $deeper plus 1,
Otherwise, the $deeper minus 1, the $deeper is 0 hits match, the div length is calculated
foreach ($sort as $key) {
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). ' </div> ';
Break
}
}
return $hitDivString;
}
echo getwebdiv (' id= ' taglist ', ' http://www.jb51.net/tag/');
end_php

Considering the ID symbol problem, the id= "U" is filled out by the user.
Disclaimer: This section of PHP is only for read with ID div content.
Perfect: Match any closed with ID tag
Copy Code code as follows:

View Code
<?php
Header ("content-type:text/html; Charset=utf-8 ");
function Getwebtag ($tag _id, $url =false, $tag = ' div ', $data =false) {
if ($url!== false) {
$data = file_get_contents ($url);
}
$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; Not hit
$divs = Array (); Merge 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 a div
$sort = Array_keys ($divs);
Asort ($sort);
$count = count ($pre _matches[0]);
foreach ($pre _matches[0] as $index => $pre _div) {
<div $hit <div+1 Div is hit
if ($pre _matches[0][$index][1] < $hit) && ($hit < $pre _matches[0][$index +1][1)) {
$deeper = 0;
Popup div before the hit Div
while (Array_shift ($sort)!= $pre _matches[0][$index][1] && ($count-)) continue;
Match the remaining Div, and if the next one is prefixed, $deeper plus 1,
Otherwise, the $deeper minus 1, the $deeper is 0 hits match, the div length is calculated
foreach ($sort as $key) {
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;
}
echo Getwebtag (' id= ' nav ', ' http://mail.163.com/html/mail_intro/', ' ul ');
echo Getwebtag (' id= ' homebanners ', ' http://mail.163.com/html/mail_intro/');
echo Getwebtag (' id= ' performance ', ' http://mail.163.com/html/mail_intro/', ' section ');
end_php

Author: Zjmainstay

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.