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);