我仍然在使用preg_match模式。我将以下字符串集存储在数组中。
$comics = array("ABSOLUTION RUBICON #4 (MR) $3.99","ABSOLUTION RUBICON #4 FINAL JUSTICE CVR (MR) $3.99","ABSOLUTION RUBICON #4 HAPPY KITTY PREMIUM CVR (MR) $9.99", "ABSOLUTION RUBICON #4 WRAP CVR (MR) $3.99","SONIC THE HEDGEHOG #253 REG CVR $2.99");
ABSOLUTION RUBICON #4 (MR) $3.99
ABSOLUTION RUBICON #4 FINAL JUSTICE CVR (MR) $3.99
ABSOLUTION RUBICON #4 HAPPY KITTY PREMIUM CVR (MR) $9.99
ABSOLUTION RUBICON #4 WRAP CVR (MR) $3.99
SONIC THE HEDGEHOG #253 REG CVR $2.99
我想做一个preg_match,以便最终列表看起来像这个
ABSOLUTION RUBICON #4 (MR) $3.99
SONIC THE HEDGEHOG #253 REG CVR $2.99
cleanComics = array("ABSOLUTION RUBICON #4 (MR) $3.99","SONIC THE HEDGEHOG #253 REG CVR $2.99");
为了给它上下文,这些是漫画标题,我想要删除变体封面,但每隔一段时间发布者就会在REG CVR中删除,我需要保留这个。
这些模式保持不变。
我认为问题编号的最后一位和$符号可能是分隔符,我只是不确定如何构建匹配模式来执行此操作并排除REG CVR案例。
应该提到这是用PHP完成的。
由于 凯文
答案 0 :(得分:1)
这就像一个魅力:D
<?
$comics = array("ABSOLUTION RUBICON #4 (MR) $3.99",
"ABSOLUTION RUBICON #4 FINAL JUSTICE CVR (MR) $3.99",
"ABSOLUTION RUBICON #4 HAPPY KITTY PREMIUM CVR (MR) $9.99",
"ABSOLUTION RUBICON #4 WRAP CVR (MR) $3.99",
"SONIC THE HEDGEHOG #253 REG CVR $2.99");
$added = Array();
$output = Array();
foreach ($comics as $title){
$temp = preg_replace("/ #([0-9])* ([\\\(]|[\\\)]|[a-z]|[ ]|[A-Z])* \\\$*([0-9])*.([0-9])*/", "", $title);
if (!in_array($temp, $added)){
$added[count($added)] = $temp;
$output[count($output)] = $title;
}
}
foreach ($output as $data){
echo "$data<br>";
}
?>
只是评论:
如果你有:
$comics = array("ABSOLUTION RUBICON #4 (MR) $3.99",
"ABSOLUTION RUBICON #5 FINAL JUSTICE CVR (MR) $3.99",
"ABSOLUTION RUBICON #6 HAPPY KITTY PREMIUM CVR (MR) $9.99",
"ABSOLUTION RUBICON #7 WRAP CVR (MR) $3.99",
"SONIC THE HEDGEHOG #253 REG CVR $2.99");
输出仍然只有2个结果。如果您想要5个结果,则需要将正则表达式更改为此结果:
$temp = preg_replace("/([\\\(]|[\\\)]|[a-z]|[ ]|[A-Z])* \\\$*([0-9])*.([0-9])*/", "", $title);
答案 1 :(得分:-1)
您可以使用preg_replace:
$result = preg_replace('~#[0-9]++ \K.*?CVR~', '', $comics);