Home  >  Article  >  Backend Development  >  Detailed explanation of regular matching of preg_match_all function in PHP_PHP tutorial

Detailed explanation of regular matching of preg_match_all function in PHP_PHP tutorial

WBOY
WBOYOriginal
2016-07-20 10:59:111379browse

The preg_match_all function is used to obtain specified data content. It is often used to perform regular expressions. Below I will introduce to you two tutorials on the implementation of the preg_match_all function. One is to obtain the url parameters, and the other is to obtain the image method in the content. ​

preg_match_all — Perform a global regular expression match

int preg_match_all ( string $pattern , string $subject [, array &$matches [, int $flags = PREG_PATTERN_ORDER [, int $offset = 0 ]]] )

Example

The code is as follows Copy code
 代码如下 复制代码

preg_match_all("|]+>(.*)]+>|U",
    "example:

this is a test ",
    $out, PREG_PATTERN_ORDER);
echo $out[0][0] . ", " . $out[0][1] . "n";
echo $out[1][0] . ", " . $out[1][1] . "n";
?>

 

preg_match_all("|]+>(.*)]+>|U",
"example:

this is a test ",
$out, PREG_PATTERN_ORDER);
echo $out[0][0] . ", " . $out[0][1] . "n";
echo $out[1][0] . ", " . $out[1][1] . "n";
?>

How to get the corresponding value of the parameter variable in the pseudo-static URL.

For example. Your current url is like this. /js/d1b3cid419Detailed explanation of regular matching of preg_match_all function in PHP_PHP tutorial99191rsGood script

Your .htaccess file will write rewirte rules like this RewriteRule ^js/(.*)$ /index.php?m=Sell&a=index&g=$1 [QSA,PT,L]
 代码如下 复制代码

$get = 'd1b3cid419Detailed explanation of regular matching of preg_match_all function in PHP_PHP tutorial99191rs好脚本';
$rs_pos = strpos($get,'rs');
if($rs_pos !== false)
{
    $rs = substr($get,$rs_pos);
    $rs = str_replace('rs','',$rs);
    $rs = strpos($rs,'/')!==false ? substr($rs,0,strpos($rs,'/')) : $rs;
    $get = substr($get,0,$rs_pos);
}
echo 'keywords='.$rs;
echo '
';
preg_match_all('/([a-z]*)([0-9]+)/',$get,$m);
if($m)
{
        $k = $v = '';
        $count = count($m[1]);
        for($i = 0; $i         {
            ${$m[1][$i]} = $m[Detailed explanation of regular matching of preg_match_all function in PHP_PHP tutorial][$i];
            if(isset(${$m[1][$i]}))
            {   
                echo $m[1][$i].'='.${$m[1][$i]};
                echo '
';
            }
        }
}

This way you can receive g.
The code is as follows Copy code
$get = 'd1b3cid419Detailed explanation of regular matching of preg_match_all function in PHP_PHP tutorial99191rsgood script';
$rs_pos = strpos($get,'rs');
if($rs_pos !== false)
{
$rs = substr($get,$rs_pos);
$rs = str_replace('rs','',$rs);
$rs = strpos($rs,'/')!==false ? substr($rs,0,strpos($rs,'/')) : $rs;
$get = substr($get,0,$rs_pos);
}
echo 'keywords='.$rs;
echo '
';
preg_match_all('/([a-z]*)([0-9]+)/',$get,$m);
if($m)
{
          $k = $v = '';
         $count = count($m[1]);
for($i = 0; $i                                   {
               ${$m[1][$i]} = $m[Detailed explanation of regular matching of preg_match_all function in PHP_PHP tutorial][$i];
                if(isset(${$m[1][$i]}))
                                                                    echo $m[1][$i].'='.${$m[1][$i]};
echo '
';
             }
         }
}

Result:keywords=good script,d=1,b=3,cid=419Detailed explanation of regular matching of preg_match_all function in PHP_PHP tutorial99191

Match images in articles

The code is as follows Copy code
 代码如下 复制代码

$con = file_get_contents("http://www.bkjia.com/");
$pattern="//";
preg_match_all($pattern,$con,$match);
print_r($match);
?>

输出代码

Array
(
[0] => Array
(
[0] =>
[1] => Detailed explanation of regular matching of preg_match_all function in PHP_PHP tutorial
[Detailed explanation of regular matching of preg_match_all function in PHP_PHP tutorial] => 875EA1C00E50B454Detailed explanation of regular matching of preg_match_all function in PHP_PHP tutorial797EDetailed explanation of regular matching of preg_match_all function in PHP_PHP tutorial4FA6E7E1FDetailed explanation of regular matching of preg_match_all function in PHP_PHP tutorial.jpg
)
[1] => Array
(
[0] => http://www.bkjia.com/uploadfile/Detailed explanation of regular matching of preg_match_all function in PHP_PHP tutorial013/0905/Detailed explanation of regular matching of preg_match_all function in PHP_PHP tutorial0130905074915895.png
[1] => http://www.bkjia.com/uploadfile/Detailed explanation of regular matching of preg_match_all function in PHP_PHP tutorial013/0905/Detailed explanation of regular matching of preg_match_all function in PHP_PHP tutorial0130905074915639.jpg
[Detailed explanation of regular matching of preg_match_all function in PHP_PHP tutorial] => http://www.bkjia.com/uploadfile/Detailed explanation of regular matching of preg_match_all function in PHP_PHP tutorial013/0905/Detailed explanation of regular matching of preg_match_all function in PHP_PHP tutorial013090507491574Detailed explanation of regular matching of preg_match_all function in PHP_PHP tutorial.jpg
)
)

$con = file_get_contents("http://www.bkjia.com/");
$pattern="//";
preg_match_all($pattern,$con,$match);
print_r($match);
?>
Output code

Array
(
[0] => Array
(
[0] =>
[1] => Detailed explanation of regular matching of preg_match_all function in PHP_PHP tutorial
[Detailed explanation of regular matching of preg_match_all function in PHP_PHP tutorial] => ; margin-right: auto;" />
)
[1] => Array
(
[0] => http://www.bkjia.com/uploadfile/Detailed explanation of regular matching of preg_match_all function in PHP_PHP tutorial013/0905/Detailed explanation of regular matching of preg_match_all function in PHP_PHP tutorial0130905074915895.png
[1] => http://www.bkjia.com/uploadfile/Detailed explanation of regular matching of preg_match_all function in PHP_PHP tutorial013/0905/Detailed explanation of regular matching of preg_match_all function in PHP_PHP tutorial0130905074915639.jpg
[Detailed explanation of regular matching of preg_match_all function in PHP_PHP tutorial] => http://www.bkjia.com/uploadfile/Detailed explanation of regular matching of preg_match_all function in PHP_PHP tutorial013/0905/Detailed explanation of regular matching of preg_match_all function in PHP_PHP tutorial013090507491574Detailed explanation of regular matching of preg_match_all function in PHP_PHP tutorial.jpg
)
)
http://www.bkjia.com/PHPjc/445630.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/445630.htmlTechArticle
preg_match_all function is used to obtain the specified data content. It is often used to execute regular expressions. Let me give Let’s introduce two tutorials on the implementation of preg_match_all function. One is to get...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn