note 89051 modified in function.urlencode by danbrown
| From: | danbrown@php.net | Date: | Fri, 20 Feb 2009 15:26:50 +0000 |
| Subject: | note 89051 modified in function.urlencode by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-150892@lists.php.net to get a copy of this message | ||
What about this one? A bit more complex but very practically,...
<?php
static function urlencode($url)
{
// safely cast back already encoded "&" within the query
$url = str_replace( "&","&",$url );
$phpsep = (strlen(ini_get('arg_separator.input')>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;
}
?>
--was--
What about this one? A bit more complex but very practically,...
<?php
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;
}
?>
http://php.net/manual/en/function.urlencode.php