分隔空格分隔的单词并删除重复的单词

发布于 2024-11-06 04:38:26 字数 268 浏览 4 评论 0原文

我在 php 中发现了 preg_splitexplode 函数,用于将空格分隔的字符串分解为单词。它也很有用。但我想要的下一步是删除重复的单词和 [",","."还有其他一些标点符号]...

就像 if
我爱尼泊尔。尼泊尔是一个内陆国家。尼泊尔位于亚洲。

我只想在句子中获得独特的单词,例如:
我爱尼泊尔,是亚洲的一个内陆国家

I found preg_split and explode functions in php for breaking space seperated string into words. its quite useful too. but the next step i would like to work is remove the duplicate words and [",","." and few other punctuation marks ] too...

like if
"I love nepal. Nepal is a landlocked country. Nepal is in Asia."

I would like to get only unique words in the sentance like:
I, love, nepal, is, a, landlocked, country, in, Asia

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

孤独患者 2024-11-13 04:38:26

这应该可以完成工作:

$string = "I love nepal. Nepal is a landlocked country. Nepal is in Asia.";
$string = preg_replace( "/[^\w\s]/", "", $string );

$words = array();
foreach ( explode( " ", $string ) AS $word ) {
    $word = strtolower($word);
    $words[$word] = 1;
}

$unique_words = array_keys( $words );

This should just about get the job done:

$string = "I love nepal. Nepal is a landlocked country. Nepal is in Asia.";
$string = preg_replace( "/[^\w\s]/", "", $string );

$words = array();
foreach ( explode( " ", $string ) AS $word ) {
    $word = strtolower($word);
    $words[$word] = 1;
}

$unique_words = array_keys( $words );
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文