-2
$string = "Apple
Foo
Banana
...
Banana
Foo
Other text
...
Apple";

I have text where single rows are duplicated after a row "...".

The rows before and after that can be anything ("Foo"), but also be a duplicate (without "..." like "Apple").

The "..."-row can appear multiple times without a duplicate row after it.

I only want to remove duplicated rows which have a "..." row inbetween.

In other words: Remove the line after "..." if it's the same as above "..."

How can I match

Banana
...
Banana

to remove the duplicated row:

Banana

so the result is

$string = "Apple
Foo
Banana
...
Foo
Other text
...
Apple";

Cheers!

8
  • 1
    what have you done 'till now? Have you any code to start from? Commented Jun 4, 2012 at 14:09
  • 2
    Foo still appears twice though Commented Jun 4, 2012 at 14:09
  • 1
    No chance here now any more, I knew people who love What have you tried will come soon, nothing to learn anything for you here these days. Commented Jun 4, 2012 at 14:10
  • Do you want to remove anything that is repeated after a "..." row? Like or does the copy have to directly above and below it? Do the repeats only show up after a "..." row? Commented Jun 4, 2012 at 14:10
  • The line with ... and the next line should be removed? Is that it? Commented Jun 4, 2012 at 14:11

4 Answers 4

1

Here is how you can remove duplicate lines in the string:

$string = implode( "\n", array_unique( explode( "\n", $string)));

explode() the string into an array on the newline, call array_unique() on the resulting array, and join the string back together with implode().

Output:

Text row A
Foo
Text row B
...
Text row C

However, note that the output doesn't perfectly match your desired output, as your desired output conflicts with your definition of the problem.

Demo

Sign up to request clarification or add additional context in comments.

1 Comment

Sorry my example was not clear enough. I only want to remove duplicated rows which have a "..." row inbetween.
1
$lines = explode("\n", $string);  
$uniqueLines = array_unique(lines);
$result = implode("\n", $uniqueLines);

1 Comment

This removes the second Foo as well, I think the OP wants something else.
0

If the task is to just remove the line following the line with three dots:

echo preg_replace("/^(.+?)\r?\n(\.{3})\r?\n\\1/m", "\\1\n\\2", $string);

The expression matches:

  • a whole line comprising at least one character (1)
  • three dots on a single line (2)
  • a whole line comprising at least one character (1)

The /m modifier is used to select multi-line mode, in which ^ and $ carry the meaning of start and end of the line.

The \\1 back reference is used to match whatever was before the three dots.

The replacement '\\1' is needed to place back the matched line with three dots.

6 Comments

Edited the answer to place the ... back into the final string
I'm sorry, the "..."-row can appear multiple times without dupe rows.
@Martin saw you chose another answer as accepted. Did my answer not work for you?
@Martin wow that's really weird, I tested it locally before I posted and it worked but it seems there's spaces after the ... and before the end-of-line marker ... strange ... updated answer
@Martin ah, of course, Windows new lines =p the joys of regexp
|
0

I'm not sure I understand all the conditions (could you have duplicates before the ... for example), but how about $string = implode("\n", array_unique(explode("\n", $string)));

Update Brute force solution:

$string = "Apple\nFoo\nBanana\n...\nBanana\nFoo\nOther text\n...\nApple\n";
$string2 = "";

$arr = explode("\n", $string);

$string2 .= $arr[0] . "\n";
$string2 .= $arr[1] . "\n";

for ($i=2; $i<count($arr); $i++)
{
    if ($arr[$i-1] != '...' || $arr[$i-2] != $arr[$i])
    {
        $string2 .= $arr[$i] . "\n";
    }

}

echo $string2;

2 Comments

Updated with a brute force solution (though I'm sure there's a neat RegExp way to solve this)
Welcome! You should probably check that the explode returns two items before blindly appending them to the string. Otherwise you'll get some funky results if the input string is empty (or doesn't contain newlines).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.