PHP foreach循环键值为$ value

时间:2016-03-11 20:58:01

标签: php html foreach preg-match echo

所以我有这个字符串:

>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个结果。我不明白为什么。

1 个答案:

答案 0 :(得分:0)

            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));