首页 > 解决方案 > Usage of "+" in array - php

问题描述

I want to write a php script which redirects to different ".txt" documents by visiting the page "http://example.com/script.php" - for example my script for "http://example.com/script.php?data=ABC+DEF". I got that far:

<?php
$targets = array("ABC+DEF" => "test.txt" /*, more redirects.. not the only one i have */ );

if (isset($_GET["data"]) && array_key_exists($_GET["data"], $targets)) {
header("Location: {$targets[$_GET["data"]]}");
exit;
?>

The issue is that it doesn't work if there is a + character in the array(""); but it works with a -.

That means "http://example.com/script.php?data=ABCDEF" would work

also "http://example.com/script.php?data=ABC-DEF" works.

Only http://example.com/script.php?data=ABC+DEF does not work.

Do I have to mark the + with a slahsh or '+' for it to work? I'm just braindamaged rightnow and not getting anywhere at this point. Probably easy to fix with two characters. Thanks for your help.


Edit: Yeah I was braindead, fixed it without even urlencode. Since I don't want to change or encode the url somehow I just leave a blank space between ABC DEF instead of using the + so it looks like this now:

array("ABC DEF" => "test.txt");

标签: phphtmlarrays

解决方案


You need to use the URL encoded character for the +, which is %2B, as without being encoded it is interpreted as a space by many web browsers


推荐阅读