note 89051 added to function.urlencode
| From: | Anonymous at osu1 dot php dot net | Date: | Thu, 19 Feb 2009 13:32:42 +0000 |
| Subject: | note 89051 added to function.urlencode | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-150851@lists.php.net to get a copy of this message | ||
What about this one? A bit more complex but very practically,...
static function urlencode($url)
{
// safely cast back already encoded "&" within the query
$url = str_replace( "&","&",$url );
$phpsep = (strlen(ini_get('arg_separator.output')>0))
?ini_get('arg_separator.output')
:"&";
// cut optionally anchor
$ancpos = strrpos($url,"#");
$lasteq = strrpos($url,"=");
$lastsep = strrpos($url,"&");
$lastqsep = strrpos($url,"?");
$firstsep = strpos($url, "?");
// recognize wrong positioned anchor example.php#anchor?asdasd
if ($ancpos !== false
|| ($ancpos > 0
&& ($lasteq > 0 && $lasteq < $ancpos )
&& ($lastsep > 0 && $lastsep < $ancpos )
&& ($lastqsep > 0 && $lastqsep < $ancpos )
)
)
{
$anc = "#" . urlencode( substr( $url,$ancpos+1 ) );
$url = substr( $url,0,$ancpos );
}
else
{
$anc = "";
}
// separate uri and query string
if ($firstsep == false)
{
$qry = ""; // no query
$urlenc = $url.$anc; // anchor
}
else
{
$qry = substr( $url, $firstsep + 1 ) ;
$vals = explode( "&", $qry );
$valsenc = array();
foreach( $vals as $v )
{
$buf = explode( "=", $v );
$buf[0]=urlencode($buf[0]);
$buf[1]=urlencode($buf[1]);
$valsenc[] = implode("=",$buf);
}
$urlenc = substr( $url, 0 , $firstsep ); // encoded origin uri
$urlenc.= "?" . implode($phpsep, $valsenc ) // encoded query string
. $anc; // anchor
}
$urlenc = htmlentities( $urlenc, ENT_QUOTES );
return $urlenc;
}
----
Server IP: 217.13.201.10
Probable Submitter: 91.11.0.109
----
Manual Page -- http://www.php.net/manual/en/function.urlencode.php
Edit -- https://master.php.net/note/edit/89051
Del: integrated -- https://master.php.net/note/delete/89051/integrated
Del: useless -- https://master.php.net/note/delete/89051/useless
Del: bad code -- https://master.php.net/note/delete/89051/bad+code
Del: spam -- https://master.php.net/note/delete/89051/spam
Del: non-english -- https://master.php.net/note/delete/89051/non-english
Del: in docs -- https://master.php.net/note/delete/89051/in+docs
Del: other reasons-- https://master.php.net/note/delete/89051
Reject -- https://master.php.net/note/reject/89051
Search -- https://master.php.net/manage/user-notes.php