简体   繁体   English

如何在PHP中删除字符串中的重复单词

[英]How to Remove repeated words in a string in php

I have a string that contains many words such as: 我有一个包含许多单词的字符串,例如:

$str = "FileZilla Portable FileZilla 3.9
        FileZilla Portable Additional Versions
        7-Zip Portable 7-Zip Portable 4.42";

I want to delete all dupplicates and return each word only once: 我想删除所有重复项,并且每个单词只返回一次:

$str = "FileZilla Portable 3.9
        FileZilla Portable Additional Versions
        7-Zip Portable 4.42";

How can I do it with php ? 我该如何用php呢?

Do this: 做这个:

$str = "FileZilla Portable FileZilla 3.9
        FileZilla Portable Additional Versions
        7-Zip Portable 7-Zip Portable 4.42";

$arr = array_unique(explode("\n", $str));
$arrLength = count($arr);
for($i = 0; $i < $arrLength; ++$i){
    $arr[$i] = implode(" ", array_unique(explode(" ", $arr[$i])));
}
$str = implode("\n", $arr);

echo $str;

Here are the relevant references: 以下是相关参考资料:

This is my code. 这是我的代码。

$str = "FileZilla Portable FileZilla 3.9 FileZilla Portable Additional Versions 7Zip Portable 7Zip Portable 4.42";

$arr = explode("\n", $str);
foreach($arr as $st) {
   echo implode(" ", array_unique(explode(" ", $st)));
}

But numbers (7Zip) not removing... 但是数字(7Zip)没有删除...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM