You can write some PHP code that will rewrite the occurrence of %2527 and %27 to an apostrophe. This should be done at the beginning of the code.
$newstring = str_replace("%2527", "\'", $_GET["q"]);
$newstring = str_replace("%27", "\'", $newstring);
So, do this:
// if navbar is needed
$q = str_replace("%2527", "\'", $q);
$q = str_replace("%27", "\'", $q);
if($pages > 1){
$links = 1;
$a = array();