簡體   English   中英

如何在PHP中刪除字符串中的重復單詞

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

我有一個包含許多單詞的字符串,例如:

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

我想刪除所有重復項,並且每個單詞只返回一次:

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

我該如何用php呢?

做這個:

$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;

以下是相關參考資料:

這是我的代碼。

$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)));
}

但是數字(7Zip)沒有刪除...

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM