Home > Article > Backend Development > Clever use of PHP functions to implement collector_PHP tutorial
After a long period of development of PHP, many users are familiar with PHP. We can now use PHP functions to implement collector programs. What is a collector? It is usually called a thief program. It is mainly used to capture the content of other people's web pages. Regarding the production of a collector, it is actually not difficult. You just need to remotely open the web page to be collected, and then use regular expressions to match the required content. As long as you have a little knowledge of regular expressions, you can make your own collector. .
This is not enough, you also need a cutting PHP function:
<ol class="dp-xml"> <li class="alt"><span><span>function cut($string,$start,$end){ </span></span></li> <li class=""> <span>$</span><span class="attribute"><font color="#ff0000">message</font></span><span> = </span><span class="attribute-value"><font color="#0000ff">explode</font></span><span>($start,$string); </span> </li> <li class="alt"> <span>$</span><span class="attribute"><font color="#ff0000">message</font></span><span> = </span><span class="attribute-value"><font color="#0000ff">explode</font></span><span>($end,$message[1]); return $message[0];}其中$string为要被切取的内容,$start为开始的地方,$end为结束的地方。取出分类号: </span> </li> <li class=""><span> </span></li> <li class="alt"> <span>$</span><span class="attribute"><font color="#ff0000">start</font></span><span> = </span><span class="attribute-value"><font color="#0000ff">"Html/Book/"</font></span><span>; </span> </li> <li class=""> <span>$</span><span class="attribute"><font color="#ff0000">end</font></span><span> </span> </li> <li class="alt"> <span>= </span><span class="attribute-value"><font color="#0000ff">"List.shtm"</font></span><span>; </span> </li> <li class=""> <span>$</span><span class="attribute"><font color="#ff0000">typeid</font></span><span> = </span><span class="attribute-value"><font color="#0000ff">cut</font></span><span>($typeid[0][0],$start,$end); </span> </li> <li class="alt"> <span>$</span><span class="attribute"><font color="#ff0000">typeid</font></span><span> = </span><span class="attribute-value"><font color="#0000ff">explode</font></span><span>("/",$typeid);[/php] </span> </li> <li class=""><span> </span></li> <li class="alt"><span>这样,$typeid[0]就是我们要找的分类号了。方法如下: </span></li> <li class=""><span> </span></li> <li class="alt"> <span>$</span><span class="attribute"><font color="#ff0000">ustart</font></span><span> = </span><span class="attribute-value"><font color="#0000ff">""</font></span><span>"; </span> </li> <li class=""> <span>$</span><span class="attribute"><font color="#ff0000">uend</font></span><span> </span> </li> <li class="alt"> <span>= </span><span class="attribute-value"><font color="#0000ff">""</font></span><span>"; </span> </li> <li class=""><span>//t表示title的缩写 </span></li> <li class="alt"> <span>$</span><span class="attribute"><font color="#ff0000">tstart</font></span><span> = </span><span class="attribute-value"><font color="#0000ff">">"</font></span><span>; </span> </li> <li class=""> <span>$</span><span class="attribute"><font color="#ff0000">tend</font></span><span> </span> </li> <li class="alt"> <span>= </span><span class="attribute-value"><font color="#0000ff">"<"</FONT></SPAN><SPAN>; </SPAN></SPAN><LI class=""><SPAN>//取路径,例如:123.shtm,2342.shtm,233.shtm </SPAN><LI class=alt><SPAN>preg_match_all("/"[0-9]{1,}.(shtm)"/is",$chapterurl,$url); </SPAN><LI class=""><SPAN>//取标题,例如:第一章 九世善人 </SPAN><LI class=alt><SPAN>preg_match_all("/</SPAN><STRONG><FONT color=#006699><SPAN class=tag><</SPAN><SPAN class=tag-name>a</SPAN></FONT></STRONG><SPAN> </SPAN><SPAN class=attribute><FONT color=#ff0000>href</FONT></SPAN><SPAN>="[0-9]{1,}.shtm"(.*?)</SPAN><SPAN class=tag><STRONG><FONT color=#006699><</FONT></STRONG></SPAN><SPAN>/a</SPAN><SPAN class=tag><STRONG><FONT color=#006699>></font></span><span>/is",$file,$title); </span> </li> <li class=""> <span>$</span><span class="attribute-value"><font color="#0000ff">count</font></span><span class="attribute"><font color="#ff0000">countcount</font></span><span> = count($url[0]); </span> </li> <li class="alt"> <span>for($</span><span class="attribute"><font color="#ff0000">i</font></span><span>=</span><span class="attribute-value"><font color="#0000ff">0</font></span><span>;$i</span><span class="tag"><strong><font color="#006699"><</font></strong></span><span>=$count;$i++) </span> </li> <li class=""><span>{ </span></li> <li class="alt"> <span>$</span><span class="attribute"><font color="#ff0000">u</font></span><span> = </span><span class="attribute-value"><font color="#0000ff">cut</font></span><span>($url[0][$i],$ustart,$uend); </span> </li> <li class=""> <span>$</span><span class="attribute"><font color="#ff0000">t</font></span><span> = </span><span class="attribute-value"><font color="#0000ff">cut</font></span><span>($title[0][$i],$tstart,$tend); </span> </li> <li class="alt"><span>$array[$u] = $t; </span></li> <li class=""><span>} </span></li> </ol>
The $array array is all the chapter addresses. At this point, the collector is half done, and the rest is Open each chapter address in a loop, read, and then match the content. This is relatively simple and will not be described in detail here. Okay, let’s end it today. This is my first time writing such a long article, so there will inevitably be problems with language organization. Please bear with me!