Mega Code Archive

 
Categories / Php / Strings
 

Remove URL Parameter Substr Strstr

<?php (REMOVE URL PARAMETER) CALLED FROM ANYWHERE function Fn_remove_url_param($In_url, $In_param_name) { trim($In_url); $qm = chr(63); // Creates a question mark $L_qm_pos = strpos($In_url, $qm) + 1; // Creates a variable that holds the position of the question mark (it may be 0 if there is no question mark) // When searching for the parameter, make sure that it is // proceeded by an & or ? and followed by an = sign $L_search_string = ("?" . $In_param_name . "="); $L_param_pos = strpos($In_url,$L_search_string); if (!empty($L_param_pos)) { // Parameter is led by ?... preserve the question mark $L_param_pos++; } else { $L_search_string = ("&" . $In_param_name . "="); $L_param_pos = strpos($In_url, $L_search_string); } if (!empty($L_param_pos)) { // The parameter exists in the param string so remove it // Find the end of the param name value pair for ($i = $L_param_pos + 1; $i <= strlen($In_url); $i++) { $L_character = (substr($In_url, $i, 1)); if ($L_character == " " or $L_character == "&") { $i ++; break; } } $In_url = substr($In_url, 0,$L_param_pos) . substr($In_url, $i - 1, strlen($In_url)); } return Fn_repair_url($In_url); // Return the page } Repair URL function Fn_repair_url($In_url) { $L_pos = strpos($In_url, "?&"); if (!empty($L_pos)) { // Remvove the & $In_url = substr($In_url, 0,$L_pos + 1) . substr($In_url, $L_pos + 2, strlen($In_url)); } $L_pos = strpos($In_url, "&&"); if (!empty($L_pos)) { // Remove one of the &s $In_url = substr($In_url, 0,$L_pos + 1) . substr($In_url, $L_pos + 2, strlen($In_url)); } if (strpos($In_url, "?") > 0 and (strpos($In_url, "?") == strlen($L_url))) { // Then the question mark that is trailing has no parameters, remove it $In_url = substr($In_url, 0, strlen($L_url - 1)); } return $In_url; } ?>