我想在=之后但在&。
之前让php进行编码$actual_link = "$_SERVER[REQUEST_URI]";
$out = substr(strstr($actual_link, '?'), strlen('?'));
$out = urlencode(base64_encode($out));
header('Location: http://'.$_SERVER[HTTP_HOST].'/workenv/img/create.php?'.$out.'');
^编码整个字符串而不是每个字符串。例如:
http://www.example.com/data.php?title=hi&apple=true
会是:
http://www.example.com/data.php?title=URLENCODEANDBASE64&apple=URLENCODEANDBASE64`
答案 0 :(得分:2)
您可以使用php
内置功能轻松完成此操作:
parse_url
分解初始网址的元素,
parse_str
分解查询字符串和
http_build_query
重建查询字符串
示例:
$url = 'http://www.example.com/data.php?title=hi&apple=true';
$parts = parse_url($url);
var_dump($parts);
/* will output
array(4) {
["scheme"]=>
string(4) "http"
["host"]=>
string(15) "www.example.com"
["path"]=>
string(9) "/data.php"
["query"]=>
string(19) "title=hi&apple=true"
}
*/
parse_str($parts["query"],$qsArray);
var_dump($qsArray);
/* will output
array(2) {
["title"]=>
string(2) "hi"
["apple"]=>
string(4) "true"
}
*/
foreach($qsArray as $key=>$value){
$encodedArray[$key]=base64_encode($value);
}
var_dump($encodedArray);
/*will output
array(2) {
["title"]=>
string(4) "aGk="
["apple"]=>
string(8) "dHJ1ZQ=="
}
*/
$encodedQS = http_build_query($encodedArray);
// http_build_query will take care of the url encoding, see below
var_dump($encodedQS);
/*will output
string(31) "title=aGk%3D&apple=dHJ1ZQ%3D%3D"
*/
$parts["query"] = "?" . $encodedQS; // prepend the "?" to the query string
$parts["scheme"] .= "://"; // append the :// bit to the scheme
$encodedURL = implode('',$parts);
var_dump($encodedURL);
/*will output
string(63) "http://www.example.com/data.php?title=aGk%3D&apple=dHJ1ZQ%3D%3D"
*/
答案 1 :(得分:0)
试试这个:
<?php
$actual_link = "http://www.example.com/data.php?title=hi&apple=true";
$out = substr(strstr($actual_link, '?'), strlen('?'));
$arr = explode('&', $out);
for($i=0;$i<count($arr);$i++){
$arr2 = explode('=', $arr[$i]);
$arr2[1] = urlencode(base64_encode($arr2[1]));
$arr[$i] = implode('=', $arr2);
}
$out = implode('&', $arr);
echo $out;
?>
答案 2 :(得分:0)
我喜欢Alex的答案的原因与我不喜欢Racil的答案的原因相同。当php提供手术刀时,无需用黄油刀进行字符串手术。
我想通过合并递归编码调用来增强Alex的答案,以适应深度可变的$_GET
数据。
代码:(Demo w/ mid-process var_export()
calls)
$url = 'http://www.example.com/data.php?key=a&array[]=b&deepArray[c][0][d][0]=x';
parse_str(parse_url($url, PHP_URL_QUERY), $queryArray);
array_walk_recursive($queryArray, function(&$v) {$v = base64_encode($v);});
echo "http://{$_SERVER['HTTP_HOST']}/workenv/img/create.php?" . http_build_query($queryArray);