// Below are a number of strings which need ammending
$string = "www.development_test.php?action=show_development_crs&test1&test2";
$string = "www.development_test.php?action=show_rfw&test1";
$string = "www.development_test.php?action=show_development_crs";
Run Code Online (Sandbox Code Playgroud)
我需要编写一个preg_replace()函数来替换" ="+" &" 之间的值,或者在底部字符串的情况下,在"="之后的所有内容,当&new不存在"newAction"时.
有没有人有任何想法?
这是我到目前为止尝试过的,但失败了:
public function test1()
{
$action = "newAction";
$string = "www.development_crs.php?action=show_development_crs&test1&test2";
$pattern = '/(action=)(.*)(&)/';
$replacement = "action=$action&";
$string = preg_replace($pattern, $replacement, $string);
echo $string;
}
Run Code Online (Sandbox Code Playgroud)
$action = 'newAction';
$string = 'www.development_crs.php?action=show_development_crs&test1&test2';
$pattern = '/action=[^&]+(.*)/';
$replacement = "action=$action$1";
$string = preg_replace($pattern, $replacement, $string);
echo $string;
Run Code Online (Sandbox Code Playgroud)
输出:
www.development_crs.php?action=newAction&test1&test2
Run Code Online (Sandbox Code Playgroud)