PHP is a simple way to get string matching content like regular

Source: Internet
Author: User
Tags explode function definition

A simple way to get string matching content is simple, easy to use, and limited in functionality

Note: See Code start Comment, there is not clear place, you can leave a message to me, welcome to the wrong, provide a good way

This example takes the http://www.3444.cc/List/List_4.htm to get the content page address for example


<?php/* Function canshujiequ ($yuanma, $CANSHUSTR, $mubiao) Description: function: Similar to get content, function is simple, can be said to be a small part of regular function but often use the function, is to implement the wildcard and custom way to get the matching content, put in the array parameter description: $yuanma-----The string from which to get the content; $canshustr-----According to the $yuanma Reed content rule, set the string: You can use the wildcard character (*), including the parameter The],[parameter is the required information extracted, $mubiao---------The combination of strings, similar to the engine collector's custom setting method, this function is also the reference locomotive that part of the set implementation method: Constant query matching location, to a unit, after acquiring, 
The string query starts the query after moving to the device until the end of the string returns the type: array containing the matched string/set_time_limit (0);
	The circular intercept function defines the start function canshujiequ ($yuanma, $canshustr, $mubiao) {if ($yuanma = = ") return array (); if (Strpos ($canshustr, ' [parameters] ') ==false| |
	Strpos ($mubiao, ' [parameter 1] ') ==false) {echo ' parameter or combination string is not formatted '; return array ();}
$CHAXUNWZ = 0;
$canshuarr =array ();
$canshuarr =explode (' [parameters] ', $canshustr);
echo ' parameter split array: ';

Print_r ($canshuarr);
Echo ' <br/><br/> ';
$len 1=count ($canshuarr);
$pipeiarr =array ();
$tpfarr =array ();
$qianks = 0;
$QIANJS = 0;
$nowks = 0;
$nowjs = 0;
$end = 0;
$num = 0; while ($end ==0) && ($chaxunwz <strlen ($yuanma)) {$mubiaofuben = $mubiao; $feikong =0, for ($i =0; ($end ==0) && ($i < $len 1); $i + +) {if($canshuarr [$i]== ') continue;
$feikong + +;
$tpfarr =explode (' (*) ', $canshuarr [$i]);
$len 2=count ($tpfarr);
$feikongnum = 0; for ($j =0; ($j < $len 2) && ($end ==0); $j + +) {if ($tpfarr [$j]== ') continue; $feikongnum + +; if ($chaxunwz >= Strlen ($yuanma)) {$end =1;break} if (($pipeiwz =strpos ($yuanma, $tpfarr [$j], $CHAXUNWZ))!==false) {$chaxunwz = $pipeiwz
+strlen ($tpfarr [$j]);
if ($feikongnum ==1) $nowks = $PIPEIWZ;


$nowjs = $CHAXUNWZ;



} else{$end =1;break;} } if ($end ==0) {if ($feikong >1) {$str =substr ($yuanma, $qianjs, $nowks-$QIANJS); $mubiaofuben =str_replace (' [Parameters '].
$feikong-1). '] ', $str, $mubiaofuben); echo replaces [parameters]. $feikong-1). ']
<br/> ';
} $qianks = $nowks;

$qianjs = $nowjs;


} Else{//echo ' did not match <br/> ';

} if ($end ==0) {$pipeiarr []= $mubiaofuben; Echo ' first '. ($num + 1). ' Result: '. $pipeiarr [$num]. '
<br/> ';
$num + +;
}}//echo "last matched array:";
Print_r ($pipeiarr);

return $pipeiarr; }//loop intercept function definition end//Below is a test example to get the page source, which matches the content page address of the movie $url = ' http://www.3444.cc/List/List_4.htm '; get the source code, use the matching function aboveGet the desired content page URL $yuanma =file_get_contents ($url); $canshustr = ' <li> <a href= '/[parameter] "target=" _blank ">"//function 1th parameter, the source code in the form of the address $mubiaostr = ' http://www.3444.cc/[ Parameter 1] ';//2nd parameter, the combined address $jieguo =canshujiequ ($yuanma, $canshustr, $MUBIAOSTR);//returns the matching array print_r ($jieguo);//Print view the effect of the capture? & Gt




Test Effect Diagram:



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.