【PHP逸出字元】單引號雙引號以及逸出字元【原創】____PHP

來源:互聯網
上載者:User

今天在寫一個指令碼,統計一個純英文的文字文件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    [capswhen] => 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;}





相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.