Convert special characters like '\ t' to "\ t" (real tab)

I want to create a CSV file importer on my website. I want the user to select a delimiter.

The problem is when the form is submitted, the separator field is stored as "\ t", for example, so when I parse the file, I look for the string "\ t" instead of the real TAB. It does the same with every special character such as \ r, \ n, etc.

I want to know a method or function to convert these characters to their true representation without using an array like:

  • 't' => "\ t"
  • 'r' => "\ r"
  • ...
+6
source share
4 answers

You should probably decide what special characters you allow and create a function like this:

function translate_quoted($string) { $search = array("\\t", "\\n", "\\r"); $replace = array( "\t", "\n", "\r"); return str_replace($search, $replace, $string); } 
+6
source
 echo str_replace("\\t", "\t", $string); 

See an example here: http://ideone.com/IVFZk

+4
source

This is like a single double quote:

 $str = strtr($str, '\t\n\r', "\t\n\r"); 
+1
source

It doesn't seem like SO leaves the tab in quotation marks, but pasting it once into any pad, then copying in quotation marks should work.

 $data = str_replace("\t", " ", $data); 
0
source

Source: https://habr.com/ru/post/888065/


All Articles