PHP for each 循环键值$value


PHP foreach loop key value to $value

所以我有这个字符串:

>Page No 1<>Page No 2<>Page No 3<

我使用正则表达式来匹配每个>Page No [number]<并将其放入数组中,然后我可以循环并显示它们:然后我将$page值写入我的文本文件中。

    $pn = '>Page No 1<>Page No 2<>Page No 3<';
    preg_match_all('/>Page No ('d+)</is', $pn, $matches);
    foreach ($matches[1] as $key => $value) {
    echo $page = '<a href="#page-'.htmlentities($value).'">' . htmlentities($value). '</a>';
    }
    $current = 'P: NAME R:';
    $PETITIONER = preg_replace('#(.*)P:(.*?)R:(.*)#is', '$2', $current);
    // Petitioner
$pj_sc_pet_dot = str_replace(array('!'s+!',':'), array(' ',''), $PETITIONER); 
$ucpj_sc_pet = ucwords(strtolower($pj_sc_pet_dot));
$pj_sc_pet_dec = htmlspecialchars_decode(trim($ucpj_sc_pet));
 $pet =  '<li class="post_petitioner">Petitioner : '.$pj_sc_pet_dec.'</li>';

    $file = 'dl/book.txt';
    $fp=fopen($file,'w+');
    $judgment = "{$page}{$pet}";
    //now, save the file
    fwrite($fp,$judgment,strlen($judgment));

但出于某种原因,我只写了 1 个结果。我不知道为什么。

            foreach ($matches[1] as $key => $value) {
        $current = 'P: NAME R:';
        $PETITIONER = preg_replace('#(.*)P:(.*?)R:(.*)#is', '$2', $current);
        // Petitioner
    $pj_sc_pet_dot = str_replace(array('!'s+!',':'), array(' ',''), $PETITIONER); 
    $ucpj_sc_pet = ucwords(strtolower($pj_sc_pet_dot));
    $pj_sc_pet_dec = htmlspecialchars_decode(trim($ucpj_sc_pet));
     $pet =  '<li class="post_petitioner">Petitioner : '.$pj_sc_pet_dec.'</li>';

$pn = '>Page No 1<>Page No 2<>Page No 3<';
        preg_match_all('/>Page No ('d+)</is', $pn, $matches);
foreach ($matches[1] as $key => $value) {
$page .= '<a href="#page-'.htmlentities($value).'">' . htmlentities($value). '</a>';
$pag ='
<li class="post_page-no">Page No : '.$page.'</li>
</ul></div></div> <!-- The Judgment Information -->
';

        $file = 'dl/book.txt';
        $fp=fopen($file,'w+');
        $judgment = "{$page}{$pet}";
    }
        //now, save the file
        fwrite($fp,$judgment,strlen($judgment));