サンプルコード
<?php
/**
* URLエンコード
* 引数:対象
* 引数:オプション(0:エンコード 1:デコード)
* 戻値:結果
*/
function _chgUrlEncode($p_sValue, $p_nFlg=0) {
$bUrl = true;
$arrUrl = getParseUrl($p_sValue);
$sScheme = '';
if (isset($arrUrl['protocol']) === true) {
$sScheme = $arrUrl['protocol'];
}
$sHost = '';
if (isset($arrUrl['authority']) === true) {
$sHost = $arrUrl['authority'];
}
if ((strlen($sScheme) === 0) || (strlen($sHost) === 0)) {
$bUrl = false;
}
$sPort = '';
$sPath = '';
if (isset($arrUrl['path']) === true) {
if ($p_nFlg === 1) {
$sPath = _chgDecode($arrUrl['path'], '/');
} else {
$sPath = _chgEncode($arrUrl['path'], '/');
}
}
$sQuery = '';
if (isset($arrUrl['query']) === true) {
if ($p_nFlg === 1) {
$sQuery = _chgDecode($arrUrl['query'], '&');
} else {
$sQuery = _chgEncode($arrUrl['query'], '&');
}
if (strlen($sQuery) > 0) {
$sQuery = '?'.$sQuery;
}
}
$sFragment = '';
if (isset($arrUrl['fragment']) === true) {
if ($p_nFlg === 1) {
$sFragment = _chgDecode($arrUrl['fragment'], '&');
} else {
$sFragment = _chgEncode($arrUrl['fragment'], '&');
}
if (strlen($sFragment) > 0) {
$sFragment = '#'.$sFragment;
}
}
//デコードエラ-
if (($sPath === false) || ($sQuery === false) || ($sFragment === false)) {
return -1;
}
if ($bUrl === false) {
return false;
}
return $sScheme.$sHost.$sPort.$sPath.$sQuery.$sFragment;
}
/**
* エンコード
* 引数:対象
* 引数:デリミタ
* 戻値:結果
*/
function _chgEncode($p_sUrl, $p_sDelimiter='&') {
$sResult = '';
$arrResult = array();
$arrUrl = explode($p_sDelimiter, $p_sUrl);
if (count($arrUrl) > 0) {
foreach ($arrUrl as $key=>$row) {
$nPos = strpos($row, '=');
if ($nPos === false) {
$arrResult[] = rawurlencode($row);
} else {
$key2 = substr($row, 0, $nPos);
$row2 = substr($row, $nPos + 1);
$arrResult[] = rawurlencode($key2).'='.rawurlencode($row2);
}
}
if (count($arrResult) > 0) {
if ($p_sDelimiter === '&') {
$p_sDelimiter = '&';
}
$sResult = implode($p_sDelimiter, $arrResult);
}
}
return $sResult;
}
/**
* デコード
* 引数:対象
* 引数:デリミタ
* 戻値:結果
*/
function _chgDecode($p_sUrl, $p_sDelimiter='&') {
$sResult = '';
$arrResult = array();
$p_sUrl = str_replace('&', '&', $p_sUrl);
$arrUrl = explode($p_sDelimiter, $p_sUrl);
if (count($arrUrl) > 0) {
foreach ($arrUrl as $key=>$row) {
$nPos = strpos($row, '=');
if ($nPos === false) {
$sWord = $row;
$sWordChk = rawurldecode($sWord);
if ($sWord !== $sWordChk) {
$sWord = $sWordChk;
}
$arrResult[] = $sWord;
} else {
$sWordKey = substr($row, 0, $nPos);
$sWordChk = rawurldecode($sWordKey);
if ($sWordKey !== $sWordChk) {
$sWordKey = $sWordChk;
}
$sWordRow = substr($row, $nPos + 1);
$sWordChk = rawurldecode($sWordRow);
if ($sWordRow !== $sWordChk) {
$sWordRow = $sWordChk;
}
$arrResult[] = $sWordKey.'='.$sWordRow;
}
}
if (count($arrResult) > 0) {
if ($p_sDelimiter === '&') {
$p_sDelimiter = '&';
}
$sResult = implode($p_sDelimiter, $arrResult );
}
}
return $sResult;
}
//エンコード
var_dump(rawurlencode('a=あ&b=い'));
var_dump(_chgUrlEncode('http://localhost/?a=あ&b=い'));
//デコード
var_dump(rawurldecode('a%3D%E3%81%82%26b%3D%E3%81%84'));
var_dump(_chgUrlEncode('http://localhost/?a=%E3%81%82&b=%E3%81%84', 1));
?>
//エンコード string(29) "a%3D%E3%81%82%26b%3D%E3%81%84" string(45) "http://localhost/?a=%E3%81%82&b=%E3%81%84" //デコード string(11) "a=あ&b=い" string(29) "http://localhost/?a=あ&b=い"