htmlspecialchars删除数组内的值? [英] htmlspecialchars remove the value inside the array?

查看:114
本文介绍了htmlspecialchars删除数组内的值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我要上传一个csv文件.其中一列带有单引号.

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

145 Test St'

数组是

(
    [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 �.

从这里开始,我使用 htmlspecialchars($ row)给出结果.

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 ( � ) ?

第二个问题,为什么使用 htmlspecialchars()后,该值消失了?

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

第三个问题,如何保留(')?

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

感谢那些可以回答的人.

Thanks for those who can answer.

  $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]);

pre()是我创建的一个功能,类似于

pre() is a function I created that works like

<pre></pre>.

我已经在终端上使用文件--mime查看了CSV文件.它的字符集是未知的8位.我通过另存为将CSV文件转换为UTF-8.之后,我设法成功上传了CSV文件.问题出在CSV文件的编码上.

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.

是否可以将文件转换为UTF-8?

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

推荐答案

根据php.net的

According to php.net's htmlspecialchars page :

如果输入字符串在给定的编码范围内包含无效的代码单元序列,除非设置了ENT_IGNORE或ENT_SUBSTITUTE标志,否则将返回一个空字符串."

"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."

因此解决方案是:使用"$ variable = htmlspecialchars($ string,ENT_IGNORE);" 您可以使用"htmlspecialchars"和类似的函数创建数组映射-

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]);

这篇关于htmlspecialchars删除数组内的值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆