Reputation: 2290
I have some functions where I need some parameters given by the user.
The parameters are mostly characters. The Problem I have is, in PHP it is pretty easy to create linebreak- and tab-characters by typing "\n"
and "\t"
.
I want the user to be able to make the same input. (typing "\n" for linebreaks ...)
The problem I am having is, that if I have a code like this:
$character1 = "\n";
$character2 = $_POST["line_break"]; // the user input is "\n"
var_dump($character1);
var_dump($character2);
The output will look like this:
string(1) "
"
string(2) "\n"
The first one is the one I want.
The only solution I could think of is checking the character for all possible inputs and replace it like this:
if ($character2 == "\\n") $character2 = "\n";
elseif ($character2 == "\\t") $character2 = "\t";
elseif ...
This seems like a pretty dirty solution. Is there any other, easyer or cleaner method to achive the result I want?
The solution can be a JavaScript solution as well if its the only way, because the user interface will work with Ajax request anyway.
Upvotes: 1
Views: 41
Reputation: 785246
You can use eval
function of PHP with care:
$character2 = eval('return "' . $_REQUEST["line_break"] . '";');
Note that since we are returning quoted value taken from $_REQUEST["line_break"]
some obvious risks of using eval
can be avoided.
Upvotes: 1