Htmlspecialchars removes a value inside an array?

So, I am downloading the csv file. One of the columns has one quote.

145 Test St' 

Array

 ( [0] => 2 [1] => 20 [2] => 145 Test St  [3] => Test City [4] => 1455 [5] => 919749797 ) 

As you can see. Instead of a single quote ( ' ), it becomes.

Here I use htmlspecialchars ($ row) which gives the result.

 ( [0] => 2 [1] => 20 [2] => [3] => Test City [4] => 1455 [5] => 919749797 ) 

The first question is why (') becomes ()?

Second question: why does the value disappear after using htmlspecialchars () ?

Third question: How to save ( ' )?

Thanks to those who can answer.

EDIT:

  $row = array_map('str_getcsv', file($_FILES['file']['tmp_name'])); $csv = Array(); $head = $row[0]; $col = count($row[0]); unset($row[0]); pre($row[1]); $row[1] = array_map('htmlentities', $row[1]); pre($row[1]); 

EDIT:

pre () is a function that I created that works like

 <pre></pre>. 

EDIT:

I am watching a CSV file using the -mime file on the terminal. Its encoding is unknown 8-bit . I am converting a CSV file to UTF-8, saving as. After that I will be able to download the CSV file. The problem is encoding the CSV file.

Can I convert a file to UTF-8?

+1
php encoding mongodb csv export-to-csv
source share
1 answer

According to the php.net htmlspecialchars page:

"If the input string contains an invalid sequence of code blocks in this encoding, an empty string will be returned if the ENT_IGNORE or ENT_SUBSTITUTE flags are not set."

So the solution: use "$ variable = htmlspecialchars ($ string, ENT_IGNORE);" You can create a function with "htmlspecialchars" and an array map that works like this:

 function specialchars($string){ return htmlspecialchars( $string, ENT_IGNORE); } $row = array_map('str_getcsv', file($_FILES['file']['tmp_name'])); $csv = Array(); $head = $row[0]; $col = count($row[0]); unset($row[0]); pre($row[1]); $row[1] = array_map('specialchars', $row[1]); pre($row[1]); 
0
source share

All Articles