【PHP转义字符】单引号双引号以及转义字符【原创】

今天在写一个脚本,统计一个纯英文的文本文档txt,里面的单词出现的数量的时候呢,代码如下:

<?php
/**
* 任一个英文的纯文本文件,统计其中的单词出现的个数。
* Created by PhpStorm.
* User: Paul
* Date: 2016/11/5
* Time: 23:18
*/
$content = file_get_contents('4/youth.txt');
$res = count_word($content, 1);
print_r($res);
/**
* 任一个英文的纯文本文件,统计其中的单词出现的个数。
* @param string $string  字符串
* @param int $lower 是否大小写   1:不区分大小写  0:区分大小写
* @return array
*/
function count_word($string, $lower = 0) {
    $string = trim($string);
    if ($lower) {
        $string = strtolower($string);
    }
    //过滤掉一些标点符号
    $string = str_replace([';', ',', '.', '‘', '?', '“', '”', '―', '-', '!', ':', '(', ')', '…', ' ', '"', '(', ')', '!', '\r', '\n'], ' ', $string);
    $array = explode(' ', $string);
    $res = array();
    foreach ($array as $value) {
        //把如I’ll、you’re、masters’s等单词后面引号的过滤掉,只留下I、you、master等单词
        if (strpos($value, '’') !== false) {
            $value = strstr($value, '’', true);
        }
        if (strpos($value, "'") !== false) {
            $value = strstr($value, "'", true);
        }
        //过滤掉空
        if (empty($value) === true) {
            continue;
        }
        if (array_key_exists($value, $res)) {
            $res[$value]++;
        } else {
            $res[$value] = 1;
        }
    }
    //排序
    array_multisort($res, SORT_DESC, SORT_NUMERIC);
    return $res;
}
运行之后呢,遇到了一种情况,会把一个单词后面换行之后接着一个单词,这两个单词会被判断成一个单词,如下:

array(
    [repression] => 1
    [thoroughness] => 1
    [bleached] => 1
    [tow] => 1
    [inspired] => 1
    [uniformwell] => 1
    [panamas] => 1
    [caps
when] => 1
)
代码中已经把\r、\n替换成空了,而且txt文件不是用windows自带的文本工具打开编辑的,是用sublime打开的并且已经设置编码为utf-8了,但还是会出现这种情况?

解决:通过在segmenfault提问以及查找一些资料才得以解决,原因是,引用转义字符的时候呢,要用双引号,不能用单引号,这个和引用变量的时候是同个道理的,比如:
<?php
$aa = '你好\r\n我不好';
echo $aa;
$bb = "你好\r\n我不好";
echo $bb;
输出:
你好\r\n我不好你好
我不好

所以,上面的代码要修改为:

<?php
/**
* 任一个英文的纯文本文件,统计其中的单词出现的个数。
* Created by PhpStorm.
* User: Paul
* Date: 2016/11/5
* Time: 23:18
*/
$content = file_get_contents('4/youth.txt');
$res = count_word($content, 1);
print_r($res);
/**
* 任一个英文的纯文本文件,统计其中的单词出现的个数。
* @param string $string  字符串
* @param int $lower 是否大小写   1:不区分大小写  0:区分大小写
* @return array
*/
function count_word($string, $lower = 0) {
    $string = trim($string);
    if ($lower) {
        $string = strtolower($string);
    }
    //过滤掉一些标点符号(注意:换行符\r、\n等必须用双引号,不能用单引号)
    $string = str_replace([';', ',', '.', '‘', '?', '“', '”', '―', '-', '!', ':', '(', ')', '…', ' ', '"', '(', ')', '!', "\r", "\n"], ' ', $string);
    $array = explode(' ', $string);
    $res = array();
    foreach ($array as $value) {
        //把如I’ll、you’re、masters’s等单词后面引号的过滤掉,只留下I、you、master等单词
        if (strpos($value, '’') !== false) {
            $value = strstr($value, '’', true);
        }
        if (strpos($value, "'") !== false) {
            $value = strstr($value, "'", true);
        }
        //过滤掉空
        if (empty($value) === true) {
            continue;
        }
        if (array_key_exists($value, $res)) {
            $res[$value]++;
        } else {
            $res[$value] = 1;
        }
    }
    //排序
    array_multisort($res, SORT_DESC, SORT_NUMERIC);
    return $res;
}





posted @ 2016-11-13 21:00  Newman·Li  阅读(1028)  评论(0编辑  收藏  举报