简体   繁体   English

如何在PHP中删除特殊引号字符?

[英]How can I strip special quote characters in PHP?

I have been handed this project that has a large number of issues with it. 我交给了这个项目,它有很多问题。 One of them is that the end-user is uploading a CSV file that is exported directly from MS Access into a directory on their web server. 其中之一是最终用户正在将直接从MS Access导出的CSV文件上载到其Web服务器上的目录中。 The next step is to truncate a couple of database tables and then insert all the records from the CSV into the database. 下一步是截断几个数据库表,然后将CSV中的所有记录插入数据库。

However, there is an issue with the apostrophe character that MS Access is using for apostrophes. 但是,MS Access用于撇号的撇号字符存在问题。 It isn't a single quote ', nor is it a double quote ". It is an apostrophe. Like this: 它不是单引号,也不是双引号。它是撇号。像这样:

"Rock/Classic 80’s-90’s"

Now, I have tried the following in my PHP to strip them out: 现在,我在我的PHP中尝试了以下内容:

$d = str_replace("’", "", $d);
$d = str_replace(array("'", "\'", "\’", "’"), "", $d);

However, this does not seem to work. 但是,这似乎不起作用。 In fact, when running SQL queries based off of this data, it always seems to somehow convert the ' into ' without stripping them out, and then causing a SQL error since it thinks that the string has been terminated early. 实际上,当基于这些数据运行SQL查询时,它总是似乎以某种方式将'转换为'而不将它们剥离出来,然后导致SQL错误,因为它认为字符串已经提前终止。

This is one of the code blocks I am using: 这是我正在使用的代码块之一:

$band_insert = "INSERT INTO `schedule` (`Band`, `Date`, `Genre`, `Club`, `Location`, `Venue`, `Time`) VALUES ( '%s', '%s', '%s', '%s', '%s', '%s', '%s' )";
$result = $mysqli->query('TRUNCATE TABLE `schedule`');
if(!$result) die('Truncate error');

if( ($handle=fopen('./export/schedule.csv', 'r')) !== FALSE)
{
    while( ($data=fgetcsv($handle, 1000, ',', '"', '\\')) !== FALSE )
    {
        foreach($data as $d) 
        {
            $d = str_replace("’", "", $d);
            # For debugging purposes only
            echo "<p>$d</p>";
        }
        $sql = sprintf($band_insert, $data[0], $data[1], $data[2], $data[3], $data[4], $data[5], $data[6]);
        #$sql = $mysqli->real_escape_string($sql);
        $result = $mysqli->query($sql);
        if( ! $result ) $log[] = lg("Unable to perform query ($mysqli->errno): $mysqli->error");
    }
    $log[] = lg("Successful upload (".date("Y-m-d").").");

    fclose($handle);
}

The question becomes, why is this not working? 问题变成了,为什么这不起作用? When I echo out the $d value, it prints a ? 当我回显$ d值时,会打印出一个? in a square. 在一个广场上。 Even with header('Content-type: text/html; charset=utf-8'); 即使有header('Content-type: text/html; charset=utf-8'); at the top of the file. 在文件的顶部。

I've had some similar hurdles with Access and Excel, and use this that I picked up somewhere to scrub the MS characters (so all due credit to it's original author). 我在Access和Excel方面遇到了一些类似的障碍,并且使用了这个,我选择了某个地方来擦除MS字符(所以应归功于它的原始作者)。 Perhaps you can use it as is, or adapt accordingly: 也许您可以按原样使用它,或相应地进行调整:

// First, replace UTF-8 characters.
$text = str_replace(
array("\xe2\x80\x98", "\xe2\x80\x99", "\xe2\x80\x9c", "\xe2\x80\x9d", "\xe2\x80\x93", "\xe2\x80\x94", "\xe2\x80\xa6"),
array("'", "'", '"', '"', '-', '--', '...'),
$text);

// Next, either REPLACE their Windows-1252 equivalents.
$text = str_replace(
array(chr(145), chr(146), chr(147), chr(148), chr(150), chr(151), chr(133)),
array("'", "'", '"', '"', '-', '--', '...'),
$text);

// OR, STRIP their Windows-1252 equivalents.
$text = str_replace(
array(chr(145), chr(146), chr(147), chr(148), chr(150), chr(151), chr(133)),
array('', '', '', '', '', '', ''),
$text);

I think there is something wrong you had written echo "<p>$d</p>"; 我认为你写的echo "<p>$d</p>";有些不对劲echo "<p>$d</p>"; .I think this should be 我认为这应该是

echo "<p>".$d."</p>";

I took your code and got it to work using a variable. 我拿了你的代码并使用变量让它工作。

$string = "Rock/Classic 80’s-90’s";
$replace = "’";
$string = str_replace($replace, "", $string);
echo "<p>$string</p>";

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM