亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

php刪除除原件外的重復項

php刪除除原件外的重復項

PHP
神不在的星期二 2022-07-22 15:11:11
這是我的代碼   <?php     $string = 'this    this     good     good     hahah';    $rows = explode("\n",$string);    $unwanted = 'this|good';    $cleanArray= preg_grep("/$unwanted/i",$rows,PREG_GREP_INVERT);    $cleanString=implode("\n",$cleanArray);    print_r ( $cleanString );?>展示hahah我想要這樣this good hahah我想保留一個...請幫助我,謝謝大家
查看完整描述

3 回答

?
慕妹3146593

TA貢獻1820條經驗 獲得超9個贊

此代碼訴諸檢查每一行以查看它是否與您的$unwanted字符串匹配,但它還會創建一個它已經遇到過的字符串數組,以便檢查之前是否遇到過( using in_array())。unset()如果它匹配并且在它用于原始$rows刪除該行之前已經遇到......


$string = 'this

    this

    good

   good

    hahah';


$rows = explode("\n",$string);

$unwanted = 'this|good';

$matched = [];

foreach ( $rows as $line => $row )   {

    if ( preg_match("/$unwanted/i",$row, $matches))  {

        if ( in_array(trim($matches[0]), $matched) === true )    {

            unset($rows[$line]);

        }

        $matched[] = $matches[0];

    }

}

$cleanString=implode("\n",$rows);

print_r ( $cleanString );


查看完整回答
反對 回復 2022-07-22
?
侃侃爾雅

TA貢獻1801條經驗 獲得超16個贊

<?php

    $string = 'this

    this 

    good

    yyyy

    good

    xxxx

    hahah';


    print_r(

       implode("\n", 

          array_diff(array_unique(

             array_map(function($v) { return trim($v);}, explode("\n",$string))

          )

       ,array('xxxx', 'yyyy')))

    );


?>

輸出:


this

good

hahah

參考:https ://ideone.com/Eo0MIM


查看完整回答
反對 回復 2022-07-22
?
HUH函數

TA貢獻1836條經驗 獲得超4個贊

這是您可以執行此操作的一種方法:


$string = 'this

    this 

    good 

    good 

    hahah';


preg_match_all('/([a-z])+/', $string, $matches);

$string = implode("\n",array_unique($matches[0]));

echo $string;


查看完整回答
反對 回復 2022-07-22
  • 3 回答
  • 0 關注
  • 135 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號