开发者

preg_replace multiple problem

开发者 https://www.devze.com 2022-12-09 00:07 出处:网络
I am trying to replace multiple urls in a pagination element with a new url. The replacement works fine when there is one occurrence of the url i.e. prev and next, but it breaks on breaks on the Pagi

I am trying to replace multiple urls in a pagination element with a new url.

The replacement works fine when there is one occurrence of the url i.e. prev and next, but it breaks on breaks on the Paginat开发者_JAVA技巧ion Numbers. It brings the first and last number links together. How do I get the preg_replace function realize that there are multiple occurrences of the links that need replacing?

<?php 
    $pattern = '/\/(.+)\/page:(\d)/S';
        $replacement = $uurl.'page:$2';
        echo preg_replace($pattern, $replacement,$paginator->prev('<< '.__('previous', true), array('url' => $paginator->params['pass']), null, array('class'=>'disabled'))).' | ';
        echo preg_replace($pattern, $replacement,$paginator->numbers());
        echo preg_replace($pattern, $replacement,$paginator->next(__('next', true).' >>', array('url' => $paginator->params['pass']), null, array('class'=>'disabled'))); 
    ?>


Try this:

$pattern = '/\/(.+?)\/page:(\d)/S';

Your .+ is greedy. In other words, it's sucking up everything between the first / and the last /page:.

The ? operator will make it match the minimum instead.

0

精彩评论

暂无评论...
验证码 换一张
取 消