简体   繁体   中英

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 ?

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...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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