3 回答
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 );
TA贡献1801条经验 获得超15个赞
<?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贡献1836条经验 获得超4个赞
这是您可以执行此操作的一种方法:
$string = 'this
this
good
good
hahah';
preg_match_all('/([a-z])+/', $string, $matches);
$string = implode("\n",array_unique($matches[0]));
echo $string;
- 3 回答
- 0 关注
- 100 浏览
添加回答
举报