preg_match在preg_match_all中

时间:2009-08-13 11:49:47

标签: php regex preg-match preg-match-all

$match_expression = '/<a href="look.php\?id=(.*)" title="Look page: (.*)">(.*)<\/A>/';
  $radompgr =  preg_match_all($match_expression,$q2,$match, PREG_SET_ORDER);
  if($radompgr == TRUE){echo "found $radompgr<br>";}else{echo "not found $radompgr<br>";} //found
for ($i = 0; $i < count($match); $i++) {
  $mathcas = $match[$i][1];
   $radom = preg_match('/[0-9a-z]{39,41}/',$mathcas,$matches2); 
   if($radom == TRUE){
   $match11 = $matches2[1];
   echo "found".$i.": ".$match11."";}else{echo"".$i."not found :(<br>";}
} //  "found0", but don`t show $match11 variable.

显示“found0”,但不要显示$ match11。如何做$ match 11来显示? 返回:

  

注意:未定义的偏移量:第75行的C:\ xampp \ htdocs \ page.php中的1   found0:   注意:未定义的偏移量:第75行的C:\ xampp \ htdocs \ copy \ page.php中的1   ,招致:   注意:未定义的偏移量:第75行的C:\ xampp \ htdocs \ copy \ page.php中的1   found2:

对不起,如果我的英语不完美,我不是本地人。 :) 谢谢您的帮助。

1 个答案:

答案 0 :(得分:3)

您忘了将()括在preg_match()中:

$match_expression = '/<a href="look.php\?id=(.*)" title="Look page: (.*)">(.*)<\/A>/';
$radompgr = preg_match_all($match_expression, $q2, $match, PREG_SET_ORDER);

if ($radompgr >= 1)
{
    echo 'found ' . $radompgr;

    for ($i = 0; $i < count($match); $i++)
    {
        $mathcas = $match[$i][1];

        $radom = preg_match('/([0-9a-z]{39,41})/', $mathcas, $matches2);

        if ($radom >= 1)
        {
            $na = $matches2[1];

            echo 'found' . $i . ': ' . $na;
        }

        else
        {
            echo $i . 'not found';
        }
    }
}

else
{
    echo 'not found ' . $radompgr;
}