Sunday, March 25, 2012

Reading delimiter tab (\t) from request with PHP


If you give to user a possibility to enter freely for example a delimiter character for csv-file, and the user selects horizontal tab, '\t', you need to change it to "real" tab character yourself in PHP, at the server side.

The inputted value "\t" is sent to server as a string, which length is 2, and contains to characters '\' AND 't'. You need to change it to one single character '\t' for example with:

$delimiter = $_REQUEST["delimiter"]; // use here the input form name you have 
$delimiter = str_replace("\\t","\t",$delimiter);

You can make a function for replacing any of the special characters you allow the user to enter.

If you are sending a pre-formatted text which contains \t characters, they remain unchanged after sending the form to the server. That is because the user did not type the characters '\' and 't', but did use a program to type a single tab character '\t'.

No comments: