开发者

How to use preg_replace to do this..?

How can I use preg_replace to do this:

I want to replace:

<a href开发者_StackOverflow社区="index.php?option=example1&view=something">

with this

<a href="index.php?option=example2&view=somethingelse&task=task1">

Thanx


you could do:

$url = '<a href="index.php?option=example1&view=something">';

$getInside = preg_match('/<a href=\"(.*)\">/', $url, $match);
$parse = parse_url($match[1]);

$newUrl = $parse['path']."?";
$urlArray = array();

parse_str($parse['query'], $params);

foreach($params as $key => $match){ 
    if($key == 'option'){
        $urlArray['option'] = 'example2';
    }
    if($key == 'view'){
        $urlArray['view'] = 'somethingelse';
    }
}
$urlArray['task'] = 'task1';
$newUrl .= http_build_query($urlArray, '', '&'); 


This will replace

<a href="index.php?option=example1&view=something">

by

<a href="index.php?option=example2&view=somethingelse&task=task1">

:

preg_replace('#<a href="index\.php\?option=example1&view=something">#', '<a href="index.php?option=example2&view=somethingelse&task=task1">', $string);
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜