3

I have about 1000 different sentences. I would like to remove the word "DLC" from these sentences, except for the fragments "All DLC BG" and "DLC Comfort" so from these two fragments the word "DLC" should not be removed.

I think array() is needed here, but I do not know how to do it.

I tried somethin like this:

if (stripos($title, 'All DLC BG') && stripos($title, 'DLC Comfort') == false) {
            $title = str_ireplace("DLC ", " ", $title);
}

but doesnt work.

laos88
  • 33
  • 3

3 Answers3

0

You are nearly there, but you need to check the Boolean condition for both operations and do it using the triple equals:

if (stripos($title, 'All DLC BG') === false && stripos($title, 'DLC Comfort') === false) {
    $title = str_ireplace("DLC ", "", $title);
}

Also I think you want to replace with empty string, and not a single space.

Dharman
  • 30,962
  • 25
  • 85
  • 135
0

If DLC can occur multiple times in a sentence, one option might be to use a regex and select those sentences using an alternation that you don't want to change and then skip those using SKIP FAIL. Then only match \bDLC\b using word boundaries.

In the replacement use an empty string.

\b(?:All DLC BG|DLC Comfort)\b(*SKIP)(*FAIL)|\bDLC\b

See a regex101 demo

For example:

$re = '/(?:All DLC BG|DLC Comfort)(*SKIP)(*FAIL)|\bDLC\b/m';
$title = 'test All DLC BG test DLC Comfort and DLC here

test here All DLC BG All DLC BG BG ALL DLC';
$result = preg_replace($re, '', $title);

echo $result;

Php demo

The fourth bird
  • 154,723
  • 16
  • 55
  • 70
-2

You could use str_replace() and pass an empty string I think. Hope this helps!