note 18746 added to function.rawurlencode

From: Date: Sun, 03 Feb 2002 19:48:27 +0000
Subject: note 18746 added to function.rawurlencode
Groups: php.notes 
Request: Send a blank email to php-notes+get-26178@lists.php.net to get a copy of this message
PHP's functions rawurlencode() and urlencode(), both encode the whole argument parameter string, making the result useless as a valid link. The function listed here encodes a link string (e.g. http://www.domain.com/long_path/to\file.php?query=param#fragm) to a valid <a href=""> parameter string, preserving the original URI structure and the path given. function linkencode ($p_url) { $ta = parse_url($p_url); if (!empty($ta[scheme])) { $ta[scheme].='://'; } if (!empty($ta[pass]) and !empty($ta[user])) { $ta[user].=':'; $ta[pass]=rawurlencode($ta[pass]).'@'; } elseif (!empty($ta[user])) { $ta[user].='@'; } if (!empty($ta[port]) and !empty($ta[host])) { $ta[host]=''.$ta[host].':'; } elseif (!empty($ta[host])) { $ta[host]=$ta[host]; } if (!empty($ta[path])) { $tu=''; $tok=strtok($ta[path], "\\/"); while (strlen($tok)) { $tu.=rawurlencode($tok).'/'; $tok=strtok("\\/"); } $ta[path]='/'.trim($tu, '/'); } if (!empty($ta[query])) { $ta[query]='?'.$ta[query]; } if (!empty($ta[fragment])) { $ta[fragment]='#'.$ta[fragment]; } return implode('', array($ta[scheme], $ta[user], $ta[pass], $ta[host], $ta[port], $ta[path], $ta[query], $ta[fragment])); } -- http://www.php.net/manual/en/function.rawurlencode.php http://master.php.net/manage/user-notes.php?action=edit+18746 http://master.php.net/manage/user-notes.php?action=delete+18746 http://master.php.net/manage/user-notes.php?action=reject+18746

« previous php.notes (#26178) next »