总结
豆包 AI 助手文章总结

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

php中文网
发布: 2016-12-01 00:56:35
原创
1265人浏览过

代码如下:

<?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;
}
登录后复制
登录后复制

输出结果:

立即学习PHP免费学习笔记(深入)”;

array(
    [repression] => 1
    [thoroughness] => 1
    [bleached] => 1
    [tow] => 1
    [inspired] => 1
    [uniformwell] => 1
    [panamas] => 1
    [caps
when] => 1
)
登录后复制
登录后复制

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

回复内容:

代码如下:

<?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;
}
登录后复制
登录后复制

输出结果:

立即学习PHP免费学习笔记(深入)”;

array(
    [repression] => 1
    [thoroughness] => 1
    [bleached] => 1
    [tow] => 1
    [inspired] => 1
    [uniformwell] => 1
    [panamas] => 1
    [caps
when] => 1
)
登录后复制
登录后复制

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

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

<?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;
}
登录后复制

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

PHP速学教程(入门到精通)
PHP速学教程(入门到精通)

PHP怎么学习?PHP怎么入门?PHP在哪学?PHP怎么学才快?不用担心,这里为大家提供了PHP速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!

下载
来源:php中文网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
豆包 AI 助手文章总结
开源免费商场系统广告
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号