PHP Picking code

Source: Internet
Author: User
PHP Capture Code
Collect http://www.01job.cn/asp/itjob.asp of the position list header three in this page
Read a lot of PHP collection tutorial or will not write so here to ask ....
Here is my code, run the wrong:
Include ("function.php");
$url = "http://www.01job.cn/asp/itjob.asp";
$ft ["title"] ["Begin"]= "
"; $ft ["title"] ["End"]= "
";
$rs =pick ($url, $ft, $th);
echo $rs ["title"];
echo "
Content: ". $rs [" content "];
?>
function.php
Get Web page Content
Function fetch_urlpage_contents ($url) {
$c =file_get_contents ($url);
return $c;
}

Get matching content
Function fetch_match_contents ($begin, $end, $c)
{
$begin =change_match_string ($begin);
$end =change_match_string ($end);
if (@preg_match ("/{$begin} (. *?) {$end}/i ", $c, $rs))
{return $rs [1];}
else {return "";}
}

Escaping the regular expression string
Function change_match_string ($STR) {
Note that the following is just a simple escape
$old =array ("/", "$");
$new =array ("\", "\$");
$str =str_replace ($old, $new, $STR);
return $str;
}

Collect Web pages
Function pick ($url, $ft, $th)
{
$c =fetch_urlpage_contents ($url);
foreach ($ft as $key = $value)
{
$rs [$key]=fetch_match_contents ($value ["Begin"], $value ["End"], $c);
if (Is_array ($th [$key]))
{foreach ($th [$key] as $old = + $new)
{
$rs [$key]=str_replace ($old, $new, $rs [$key]);
}
}
}
return $rs;
}

?>


------Solution--------------------
Get Web page Content
Function fetch_urlpage_contents ($url) {
for ($i =0; $i <10; $i + +)
{
$c = @file_get_contents ($url);
if (Trim ($c)! = "") break;
}
Print ($c);
return $c;
}

Get matching content
Function fetch_match_contents ($begin, $end, $c)
{
$beginPos = Strpos ($c, $begin);
$endPos = Strpos ($c, $end);
if ($beginPos > 0 && $endPos > 0 && $endPos > $beginPos)
{
$result = substr ($c, $beginPos +strlen ($begin), $endPos-$beginPos-strlen ($begin));
return $result;
}
Else
{
Return "";
}
}

Collect Web pages
Function pick ($url, $ft, $th)
{
$c =fetch_urlpage_contents ($url);
foreach ($ft as $key = $value)
{
$rs [$key]=fetch_match_contents ($value ["Begin"], $value ["End"], $c);
if (Is_array ($th [$key]))
{foreach ($th [$key] as $old = + $new)
{
$rs [$key]=str_replace ($old, $new, $rs [$key]);
}
}
}
return $rs;
}
?>


<title>Caiji</title>





$url = "http://www.01job.cn/asp/itjob.asp";
$ft ["title"] ["Begin"]= " <title>"; <br/> $ft ["title"] ["End"]= "</title>";
$rs =pick ($url, $ft, $th);
Print_r ($RS);
?>


------Solution--------------------
Write an example:
PHP code
 $content = file_get_contents (' http://www.01job.cn/ Asp/itjob.asp ');p reg_match_all ('/(. *) <\/a>/', $content, $arr);p Rint_r ($arr); 
  • 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.