htmlspecialchars remove the value inside the array?

人走茶凉 提交于 2019-12-13 17:28:29

问题


So I'm uploading a csv file. One of the column have a single quote.

145 Test St'

The array is

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

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

From here, I use htmlspecialchars($row), which gives the result.

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

First question, why ( ' ) becomes ( � ) ?

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

Third question, How can I retain the ( ' ) ?

Thanks for 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 I created that works like

<pre></pre>.

EDIT:

I've look at the CSV file using file --mime at the terminal. It's charset is unknown 8-bit. I convert the CSV file to UTF-8 by doing a save as. After that I manage to upload the CSV file successfully. The problem is on the encoding of the CSV file.

Is it possible to convert the file into UTF-8?


回答1:


According to php.net's htmlspecialchars page :

"If the input string contains an invalid code unit sequence within the given encoding an empty string will be returned, unless either the ENT_IGNORE or ENT_SUBSTITUTE flags are set."

So the solution is: use "$variable = htmlspecialchars( $string, ENT_IGNORE);" You can create a function with "htmlspecialchars" and array map that function 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]);


来源:https://stackoverflow.com/questions/46823873/htmlspecialchars-remove-the-value-inside-the-array

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!