class_mcUrl.inc.php 1.38 KB
<?php
/** 
 * @copyright (c) 2014, netz.coop eG
 */
class mcUrl {
	/**
	 * 
	 * @param string $param_url  example: domain.lc/?var1=1
	 * @param array $param_Vars example: array('var1'=>1)
	 * @return string
	 */
	public static function removeVarFromUrlstring($param_url, array $param_Vars) {
		foreach($param_Vars as $key => $value) {
			if(strpos($param_url, '?'.$key.'=') )  {
				$seperator = '?';
			} else if(strpos($param_url, '&'.$key.'=')) {
				$seperator = '&';
			}					
			$Url = substr($param_url, 0, strpos($param_url, $seperator.$key.'='.$value));
			if($rest = substr($param_url, strpos($param_url, $seperator.$key.'='.$value) + strlen($seperator.$key.'='.$value))) {
				if(strpos($Url, '?')===false) {
					$Url = $Url.'?'.substr($rest, 1);
				} else {
					$Url = $Url.'&'.substr($rest, 1);
				}
			}
			$param_url = $Url;
		}
		return $param_url;
	}

	/**
	 * 
	 * @param string $param_url  example: domain.lc/?var1=1
	 * @param array $param_Vars example: array('var1'=>1)
	 * @return string
	 */	
	public static function addVarToUrlstring($param_url, array $param_Vars) {
		if(strpos($param_url, '?')===false && strpos($param_url, '&')===false) {
			$onekey = key($param_Vars);
			$param_url .= '?'.$onekey.'='.$param_Vars[$onekey];
			unset($param_Vars[$onekey]);
		} 
		foreach($param_Vars as $key => $value) {
			$param_url .= '&'.$key.'='.$value;
		}		
		
		return $param_url;
	}

}
?>