Php crawls the page content first to capture a useful link in the page, then captures the content of the link on the page, and uses the for loop, but an error occurs when the loop reaches the second time,
After thinking for a long time, I don't know where the problem is. please take a look.
$ Url = 'http: // www.meishij.net/chufang/diy /? Page = 1 # listnav '; $ opts = array ('http' => array ('User _ agent' => "Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0) ",); $ context = stream_context_create ($ opts); $ con = file_get_contents (" $ url ", false, $ context); // entry $ preg = '#(.*)# '; Preg_match_all ($ preg, $ con, $ arr); // assign the search content to the array for ($ I = 0; $ I <20; $ I ++) // capture a single page {// print_r ($ arr [0] [$ I]); $ ss = $ arr [2] [$ I]; echo $ ss; echo"
"; $ Opts = array ('http' => array ('User _ agent' =>" Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0 )",)); $ context = stream_context_create ($ opts); $ cons = file_get_contents ("$ ss", false, $ context); // Title $ preg = '#(. *) # '; preg_match_all ($ preg, $ cons, $ arr); // assign the search content to the array print_r ($ arr [0] [0]); // content $ preg = '#(.*)(.*)
# '; Preg_match_all ($ preg, $ cons, $ arr); // assign the search content to the array print_r ($ arr [0] [0]); echo"
"; Print_r ($ arr [0] [1]); echo"
"; Print_r ($ arr [0] [2]); echo"
"; // Image $ preg = '## '; Preg_match_all ($ preg, $ cons, $ arr); // assign the search content to the array print_r ($ arr [0] [0]); echo"
"; Print_r ($ arr [0] [1]); echo"
"; Print_r ($ arr [0] [2]); echo"
"; Print_r ($ arr [0] [3]); echo"
"; Print_r ($ arr [0] [4]); echo"
"; Print_r ($ arr [0] [5]); echo"
"; Print_r ($ arr [0] [6]);}
Reply to discussion (solution)
Which of the following Experts knows how to tell 3Q,
Solve the problem.
Print_r $ arr; // check the content of $ arr.
Preg_match_all ($ preg, $ con, $ arr); // assign the search content to the array
For ($ I = 0; $ I <20; $ I ++) // capture a single page
{
// Print_r ($ arr [0] [$ I]);
$ Ss = $ arr [2] [$ I];
Echo $ ss;
Echo"
";
$ Opts = array (
'Http' => array (
'User _ agent' => "Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0 )",
)
);
$ Context = stream_context_create ($ opts );
$ Cons = file_get_contents ("$ ss", false, $ context );
Print_r ($ cons );
Die ();
// Title
$ Preg = '#(.*)#';
Preg_match_all ($ preg, $ cons, $ arr); // assign the search content to the array
Print_r ($ arr [0] [0]);
// Content
$ Preg = '#
(.*)(.*)
#';
Preg_match_all ($ preg, $ cons, $ arr); // assign the search content to the array
Print_r ($ arr [0] [0]);
Echo"
";
Looking at the red part above, the array names inside and outside the loop are the same. Is there no problem?