• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

换行 – 【PHP】在用PHP来统计一个纯英文的txt的单词的时候,为什么会这种情况?【已解决】

php 搞代码 3年前 (2022-01-22) 24次浏览 已收录 0个评论
文章目录[隐藏]

代码如下:

<code><?php/*** 任一个英文的纯文本文件,统计其中的单词出现的个数。* Created by PhpStorm.* User: Paul* Date: 2016/11/5* Time: 23:18*/$content = file_get_contents('4/Gone with the wind.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);    $string = str_replace(',', '', $string);    $string = str_replace('.', '', $string);    $string = str_replace('.', '', $string);    $string = str_replace('‘', '', $string);    $string = str_replace('?', '', $string);    $string = str_replace('“', '', $string);    $string = str_replace('”', '', $str<div style="color:transparent">本文来源gaodai.ma#com搞##代!^码@网*</div><pre>搞gaodaima代码

ing); $string = str_replace(‘―’, ”, $string); $string = str_replace(‘-‘, ”, $string); $string = str_replace(‘!’, ”, $string); $string = str_replace(‘:’, ”, $string); $string = str_replace(‘(‘, ”, $string); $string = str_replace(‘)’, ”, $string); $array = explode(‘ ‘, trim($string)); $res = array(); foreach ($array as $key=>$value) { //过滤掉如I’ll、you’re、masters’s等单词 if (strpos($value, ‘’’) !== false || strpos($value, “‘”) !== false) { continue; } //过滤掉空 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;}

输出结果:

<code>array(    [repression] => 1    [thoroughness] => 1    [bleached] => 1    [tow] => 1    [inspired] => 1    [uniformwell] => 1    [panamas] => 1    [capswhen] => 1)</code>

不明白为什么会把两个单词给判断成一个单词,txt呢是用sublime打开并且设置编码为UTF-8,没有用电脑自带的文本文档工具打开编辑过,另外呢,过滤标点符号的时候也有加上过滤掉rn来处理,但是没效果,所以代码去掉了。求解为什么会出现这种情况并且如何避免?

回复内容:

代码如下:

<code><?php/*** 任一个英文的纯文本文件,统计其中的单词出现的个数。* Created by PhpStorm.* User: Paul* Date: 2016/11/5* Time: 23:18*/$content = file_get_contents('4/Gone with the wind.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);    $string = str_replace(',', '', $string);    $string = str_replace('.', '', $string);    $string = str_replace('.', '', $string);    $string = str_replace('‘', '', $string);    $string = str_replace('?', '', $string);    $string = str_replace('“', '', $string);    $string = str_replace('”', '', $string);    $string = str_replace('―', '', $string);    $string = str_replace('-', '', $string);    $string = str_replace('!', '', $string);    $string = str_replace(':', '', $string);    $string = str_replace('(', '', $string);    $string = str_replace(')', '', $string);    $array = explode(' ', trim($string));    $res = array();    foreach ($array as $key=>$value) {        //过滤掉如I’ll、you’re、masters’s等单词        if (strpos($value, '’') !== false || strpos($value, "'") !== false) {            continue;        }        //过滤掉空        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;}</code>

输出结果:

<code>array(    [repression] => 1    [thoroughness] => 1    [bleached] => 1    [tow] => 1    [inspired] => 1    [uniformwell] => 1    [panamas] => 1    [capswhen] => 1)</code>

不明白为什么会把两个单词给判断成一个单词,txt呢是用sublime打开并且设置编码为UTF-8,没有用电脑自带的文本文档工具打开编辑过,另外呢,过滤标点符号的时候也有加上过滤掉rn来处理,但是没效果,所以代码去掉了。求解为什么会出现这种情况并且如何避免?

你的问题应该就出在没有处理换行(和回车)以及那些过滤字符被替换成了”, 应该替换成’ ‘

<code class="php"><?php$content = file_get_contents(__FILE__); //没有你的原始文本, 所以就直接读取文件自身作为样本了$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 $key=>$value) {        //过滤掉空        if (!$value) {            continue;        }        //过滤掉如I’ll、you’re、masters’s等单词        if (strpos($value, '’') !== false || strpos($value, "'") !== false) {            continue;        }        if (array_key_exists($value, $res)) {            $res[$value]++;        } else {            $res[$value] = 1;        }    }    //排序    array_multisort($res, SORT_DESC, SORT_NUMERIC);    return $res;}</code>

不知道你的文件里的字符串是什么样子的,不过trim函数只会去掉两边的空格(rn),感觉问题会出在这里。


搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:换行 – 【PHP】在用PHP来统计一个纯英文的txt的单词的时候,为什么会这种情况?【已解决】

喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址