zoukankan      html  css  js  c++  java
  • 【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([';', ',', '.', '‘', '?', '“', '”', '―', '-', '!', ':', '(', ')', '…', ' ', '"', '(', ')', '!', '
    ', '
    '], ' ', $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
    )
    代码中已经把 、 替换成空了,而且txt文件不是用windows自带的文本工具打开编辑的,是用sublime打开的并且已经设置编码为utf-8了,但还是会出现这种情况?

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

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

    <?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([';', ',', '.', '‘', '?', '“', '”', '―', '-', '!', ':', '(', ')', '…', ' ', '"', '(', ')', '!', "
    ", "
    "], ' ', $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;
    }





  • 相关阅读:
    2017博普杯 东北大学邀请赛(B. Drink too much water)(贪心+树链剖分)
    AGC018D Tree and Hamilton Path(树+树的重心)
    BZOJ2843:极地旅行社
    P++ 1.0.5
    BZOJ1052:[HAOI2007]覆盖问题
    BZOJ3098:Hash Killer II
    BZOJ2784:[JLOI2012]时间流逝
    BZOJ2282:[SDOI2011]消防
    BZOJ1875:[SDOI2009]HH去散步
    Codeforces 504 A (Round #285 div.1 A) Misha and Forest
  • 原文地址:https://www.cnblogs.com/linewman/p/9918127.html
Copyright © 2011-2022 走看看