如何使用preg_replace执行此操作:
我想替换:
<a href="index.php?option=example1&view=something">
用这个
<a href="index.php?option=example2&view=somethingelse&task=task1">
感谢名单
答案 0 :(得分:1)
$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, '', '&');
答案 1 :(得分:0)
这将取代
<a href="index.php?option=example1&view=something">
通过
<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);