You have stated in your question that you wish to REMOVE page=xx from
the URL.
There's Nothing like the smell of regex in the afternoon... So I had a stab at this and the following should do the trick.
I'm sure it can be done more smartly, but it gets you up and running.
<?php
/*
* My urls can be
* pagename?page=2 or
* pagename?param=235235&page=2 or
* pagename?page=10¶m=5431 or etc.
*/
function remove_page_from_url($url){
// For the case where ?page=xx¶m
if (preg_match('/\?page=[0-9]{0,}&/',$url)){
$url = preg_replace('/&/','?',$url,1); // Only replace the 1st one.
}
return preg_replace('/[\?|&]page=[0-9]{0,}/','',$url);
}
// The Testing during development
//Case 1: This works
$url = 'pagename?page=10';
echo remove_page_from_url($url);
echo '<br>';
//Case 2: This works
$url = 'pagename?param=235235&page=2';
echo remove_page_from_url($url);
echo '<br>';
//Case 3: This works
//This messes things up as we also have to change a & to a ?
$url = 'pagename?page=10¶m=5431';
echo remove_page_from_url($url);
echo '<br>';
//Case 4: This Works
//This messes things up as we also have to change a & to a ? but only the first one.
$url = 'pagename?page=10¶m=5431&something=2';
echo remove_page_from_url($url);
echo '<br>';
So you start from the simplest case and work your way up.
/*
The Results
pagename?page=10
=> pagename
pagename?param=235235&page=2
=> pagename?param=235235
pagename?page=10¶m=5431
=> pagename?param=5431
pagename?page=10¶m=5431&something=2
=> pagename?param=5431&something=2
*/
So it is the code inside function remove_page_from_url($url) that you are interested in.