I need to replace link index.php?a=2 ,but if it’s have & after a=2 then don’t replace it.
index.php?a=2 <- replace
index.php?a=2&b=3 <- don't replace
Using PHP, preg_replace()
I have tried using this:
preg_replace('^index\.php\?a=([0-9]+)(?!amp;)^', 'home', 'index.php?a=2');
preg_replace('^index\.php\?a=([0-9]+)(?!amp;)^', 'home', 'index.php?a=2&b=3');
preg_replace('^index\.php\?a=([0-9]+)(?!&)^', 'home', 'index.php?a=2');
preg_replace('^index\.php\?a=([0-9]+)(?!&)^', 'home', 'index.php?a=2&b=3');
That I’m using:
preg_replace('^(\/*)index\.php\?a=([0-9]+)(?!&)^e', "a($2)", 'index.php?a=2');
function a($id) {
// Getting name from mysql...
return '/a$id_$name';
}
It’s working with this: index.php?a=2&b0, but with index.php?a=10&b=0 not.
PHP Script:
Output:
Test the code here.