I am trying to convert special characters (eg. +
, /
, &
, %
) which I will use them for a GET request. I have constructed a
There is a built-in PHP function for this, which is a far better option than doing it manually.
urlencode - built in function from php
However, if you still want to do it manually:
function convert_text($text) {
$t = $text;
$specChars = array(
'!' => '%21', '"' => '%22',
'#' => '%23', '$' => '%24', '%' => '%25',
'&' => '%26', '\'' => '%27', '(' => '%28',
')' => '%29', '*' => '%2A', '+' => '%2B',
',' => '%2C', '-' => '%2D', '.' => '%2E',
'/' => '%2F', ':' => '%3A', ';' => '%3B',
'<' => '%3C', '=' => '%3D', '>' => '%3E',
'?' => '%3F', '@' => '%40', '[' => '%5B',
'\\' => '%5C', ']' => '%5D', '^' => '%5E',
'_' => '%5F', '`' => '%60', '{' => '%7B',
'|' => '%7C', '}' => '%7D', '~' => '%7E',
',' => '%E2%80%9A', ' ' => '%20'
);
foreach ($specChars as $k => $v) {
$t = str_replace($k, $v, $t);
}
return $t;
}
place the key and value in the last so that the last iteration of loop will be for spaces
Do not use a loop.
Use str_replace
str_replace($entities, $replacements, $string);
Or better use this native PHP function rawurlencode
You can use rawurlencode($url)
builtin function.
<?php
$url='http://fb.com';
echo rawurlencode($url);
the output:
http%3A%2F%2Ffb.com
Because all your replacements have a percentage (%) character in them, you have to first replace all the occurrences of % with its equivalent %25. Afterwards you should replace other characters such as space and so on. An when you try to convert back the encoded url to its original state, % character should be the last character that you convert back. Convert back your array in reverse order. use array_reverse() function to reverse the order of elements of $specChars. In other words you have to use two separate functions for encoding and decoding your urls.
The following functions are the ones I have written myself to do the job.
$arrayChrs = array('%',' ','!');
$arrayChrs = array('%25', '%20','%21');
function encodeData($subject)
{ //enode data
global $arrayCodes;
global $arrayChrs;
if (!is_null($arrayChrs) && !is_null($arrayCodes)) {
$arCnt1 = count($arrayChrs);
$arCnt2 = count($arrayCodes);
if ($arCnt2 == $arCnt1) {
for ($x = 0; $x <= ($arCnt2 - 1); $x++) {
$code1 = $arrayCodes[$x];
$char1 = $arrayChrs[$x];
$code_encoded = utf8_encode($code1);
$char_encoded = utf8_encode($char1);
$subject = str_replace($char_encoded, $code_encoded, $subject);
}
}
}
return $subject;
}
//[end function]
function decodeData($subject)
{ //decode data
global $arrayCodes;
global $arrayChrs;
$outputStr = $subject;
if (!is_null($arrayChrs) && !is_null($arrayCodes)) {
$arCnt1 = count($arrayChrs);
$arCnt2 = count($arrayCodes);
if ($arCnt2 >= 1 and $arCnt2 == $arCnt1) {
if ($arCnt2 >= 1) {
for ($x = ($arCnt2-1); $x >=0; $x--) {
$code1 = $arrayCodes[$x];
$char1 = $arrayChrs[$x];
$code_encoded = utf8_encode($code1);
$char_encoded = utf8_encode($char1);
$outputStr = str_replace($code_encoded, $char_encoded, $outputStr);
}
}
}
}
return $outputStr;
}
//[end function ]
Notice that one for loop counts elements forward by using $x++, and the other one counts elements in reverse order by using $x--.
Elements in $arrayChrs are replaced by elements in $arrayCodes during encoding. During decoding the reverse replacement takes place.
Wouldn't you be better off using the PHP build in functionality to do this: rawurlencode?
There's already a function for this in the standard PHP library: rawurlencode.