Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I want to make a CSV file importer on my website. I want the user to choose the delimiter.

The problem is when the form submits, the delimiter field is stored as '\t', for example, so when I'm parsing the file, I search for the string '\t' instead of a real TAB. It does the same thing with every special characters like \r, \n, etc...

I want to know the way or the function to use to convert these characters to their true representation without using an array like:

  • 't' => "\t"
  • 'r' => "\r"
  • ...
like image 927
Marm Avatar asked May 13 '11 19:05

Marm


People also ask

What is the Unicode for tab?

The tab character (unicode U+0009) is typically converted to spaces (unicode U+0020) by the white space processing rules and then collapsed so that only one space in a row is displayed in the browser.

How do I make a tab character in HTML?

Adding Tab Space in HTML Unlike with HTML space, there is no particular HTML tab character you could use. You could technically use the 	 entity as the tab is character 9 in the ASCII.


1 Answers

You should probably decide what special chars will you allow and create a function like this one:

function translate_quoted($string) {
  $search  = array("\\t", "\\n", "\\r");
  $replace = array( "\t",  "\n",  "\r");
  return str_replace($search, $replace, $string);
}
like image 164
Sebastián Grignoli Avatar answered Sep 24 '22 12:09

Sebastián Grignoli