《Disney +》 迪士尼、Marvel、彼思、星球大戰…  《State of Play》將於2月26日本週五精彩回歸   爭奪寶座:加入我們最新的PlayStation社群活動,就有機會贏取PS5主機一台等各種獎項   「快打旋風 V」季票5 開始更新! 《彈》 隆重登場!並實裝《V-shift》系統!   《KartRider: Drift》2022年飆速登陸PS4   大人氣的Bauhutte電競椅進化!2種不同質料的「Gaming Sofa Chair 2」登場!   日本最大數位教育中心「REDEE」開設體驗VR的HTC「VIVE」專區!   第27回TETRIS王者盃「瑪利歐派對 超級巨星合作祭!」舉辦決定!   搶先看《Apex 英雄》賽季 9的 3V3模式「競技場」與英雄「瓦爾基里」等新要素! 

PHP 將陣列有部份「符合字串」的全部過濾移除

商業

PHP 要對陣列的內容做過濾排除的動作,類似 grep -v 的效果,要怎麼做呢?

  • 註:grep -v:--invert-match (Invert the sense of matching, to select non-matching lines.)

PHP 將陣列有部份「符合字串」的全部過濾移除

本來想用 array_filter() 來做,不過覺得殺雞用牛刀,自己寫個簡單的處理即可。

此 Function 是實做 grep -iv 的作法,不想忽略大小寫的話(grep -v),將 stristr 改成 strstr 即可。

 $line) {
        foreach ($filter_row as $filter) {
            if (stristr($line, $filter) !== false) {
                unset($lines[$i]);
                continue;
            }
        }
    }

    return $lines;
}

$lines[] = 'e aa djkl';
$lines[] = 'bb djkl';
$lines[] = 'cc abja';
$lines[] = 'aa dd abja';

$r = grep_filter($lines, ['aa', 'bb']);

print_r($r); // cc abja
?>
Tsung

隨機商業新聞