4 回答

TA貢獻1825條經驗 獲得超6個贊
此代碼用于檢查每行以查看它是否與您的字符串匹配,但它也會創建一個已遇到的字符串數組,以便檢查以前是否遇到過(使用)。如果它匹配并且已經遇到之前,它在原始使用中刪除該行...$unwantedin_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 );

TA貢獻2037條經驗 獲得超6個贊
<?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

TA貢獻1811條經驗 獲得超5個贊
以下是您可以執行此操作的一種方法:
$string = 'this
this
good
good
hahah';
preg_match_all('/([a-z])+/', $string, $matches);
$string = implode("\n",array_unique($matches[0]));
echo $string;

TA貢獻1847條經驗 獲得超11個贊
您可以使用php內置函數array_unique
<?php
$string = 'this
this
good
good
haha';
$rows = explode("\n",$string);
$cleanArray = array_unique($rows);
$cleanString=implode("\n",$cleanArray);
print_r ( $cleanString );
//result is this good haha
- 4 回答
- 0 關注
- 118 瀏覽
添加回答
舉報