PHP Simulation Grab the page some functions to organize

Source: Internet
Author: User
Tags explode

Curl simulates submitting post data

/* $url @ Submit Address $postdata Example $postdata = Array (' key ' = ' value '), */function curlpost ($url, $postData =array ()) {if (Empty ($ URL)) return false; $o = ""; foreach ($postData as $k = = $v) {$o. = "$k =". UrlEncode ($v). " & ";} $postData =substr ($o, 0,-1); $ch = Curl_init (); $timeout = 5; curl_setopt ($ch, Curlopt_returntransfer, 1); curl_setopt ($ch, Curlopt_connecttimeout, $timeout); curl_setopt ($ch, Curlopt_post, 1); curl_setopt ($ch, Curlopt_header, 0); curl_setopt ($ch, Curlopt_url, $url); curl_setopt ($ch, Curlopt_postfields, $postData); $contents = Curl_exec ($ch); return $contents;}

CURL Crawl Page

/* * $url @ Grab page address */function getcontents ($url) {$ch = Curl_init (); $timeout = 5; curl_setopt ($ch, Curlopt_url, $url); CU Rl_setopt ($ch, Curlopt_returntransfer, 1); curl_setopt ($ch, Curlopt_connecttimeout, $timeout); $contents = curl_exec ($ch); return $contents;}

Regular grabs to tabel content

/* * $table  The content crawled over the page */function Get_td_array ($table) {//Remove HTML Tag Properties $table = Preg_replace ("' <table[^>]*? > ' si ', "", $table); $table = Preg_replace ("' <tr[^>]*?> ' si '," ", $table); $table = Preg_replace (" ' <td[^ >]*?> ' Si ', "", $table), $table = Str_replace ("</tr>", "{tr}", $table) $table = Str_replace ("</td>", "{ TD} ", $table);//Remove HTML Tag $table = preg_replace (" ' <[\/\!] *? [^<>]*?> ' si ', ' ", $table);//Remove the whitespace character $table = Preg_replace (" ' ([\ r \ n]) [\s]+ ' "," ", $table); $table = Str_replace ("  "," ", $table); $table = Str_replace (" "," "", $table) $table = Explode (' {tr} ', $table); Array_pop ($table); foreach ($table as $key = $tr) {$td = explode (' {TD} ', $TR); Array_pop ($TD); $TD _array[] = $td;} return $TD _array;}

PHP Simulation Grab the page some functions to organize

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.