PHP从一个大段落替换小段落中的行

I have 2 bulks of text: Trunk, and Card. Trunk has about 100 lines, and Card has 3. The 3 lines in Card exist in Trunk, but they aren't directly below eachother.

What I'm trying to do is remove each line of Card from the string Trunk.

What came to mind is exploding Card into an Array and using a for each in loop, like in AS3, but that didn't work like planned. Here's my attempt:

$zarray = explode("
", $card); //Exploding the 3 lines which were seperated by linebreaks into an array

foreach ($zarray as $oneCard) //for each element of array
{
    $oneCard.= "
"; //add a linebreak at the end, so that when the text is removed from Trunk, there won't be an empty line in it.
    print "$oneCard stuff"; //Strangely outputs all 3 elements of the array seperated by , instead of just 1, like this:
    //card1card2card3 stuff
    $zard = preg_replace("/$oneCard/i", "", $trunx, 1);//removes the line in Card from Trunk, case insensitive.
    $trunx = $zard; //Trunk should now be one line shorter.
}

So, how can I use the foreach loop so that it replaces properly, and uses 1 element each time, instead of all of them in one go?

Consider

$trunk = "
a
b
c
d
e
f
";

$card = "
c
e
a
";


$newtrunk = implode("
", array_diff(
    explode("
", $trunk),
    explode("
", $card)
));
print $newtrunk; // b d f

Or the other way round, your wording is a bit unclear.

Try this, it will be faster than the preg_replace due to the small amount being replaced:

//Find the new lines and add a delimiter
$card = str_replace("
", "
|#|", $card);

//Explode at the delimiter
$replaceParts = explode('|#|', $card);

//Perform the replacement with str_replace and use the array
$text = str_replace($replaceParts, '', $text);

This assumes there is always a newline after the search part and you do not care about case sensitivity.

If you do not know about the new line you will need a regex with an optional match for the newline.

If you need it case sensitive, look at str_ireplace

You could explode the $card, and keep the $trunk as string:

$needlearray = explode("
", $card); //to generate the needle-array
$trunk = str_replace($needlearray,array(),$trunk); //replace the needle array by an empty array => replace by an empty string (according to the str_replace manual)
$trunk = str_replace("

","
",$trunk); //replace adjacent line breaks by only one line break