Split email with rules

I have to split an email that is received everyday, with a set of rules. This is an example of the email:

A N K U N F T 11.08.15
*** NEUBUCHUNG ***
11.08.15 xxx xxx X3 2830 14:25 17:50
18.08.15 xxx xxx X3 2831 18:40
F882129 dsdsaidsaia
F882129 xxxyxyagydaysd

«CUT HERE»

A N K U N F T 18.08.15
*** NEUBUCHUNG ***
11.08.15 xxx xxx X3 2830 14:25 17:50
18.08.15 xxx xxx X3 2831 18:40
F881554 ZXCXZCXCXZCCXZ
F881554 xcvcxvcxvcvxc
F881554 xvcxvcxcvxxvccvxxcv

«CUT HERE»

11.08.15 xxx xxx X3 2830 14:25 17:50
18.08.15 xxx xxx X3 2831 18:40
F881605 xczxcdfsfdsdfs
F881605 zxccxzxzdffdsfds

«CUT HERE»
So it basically has to be cut whenever the last F999999 appears ( where 9 can be any number). How can I do this?

I tried the solution on a regex101.com - https://regex101.com/r/rT0yQ1/1 and it worked, however when I do this on my server with php and sending the email text by post is doesnt. I paste the email text to a textbox and then press a button that then divides it. Here is my code: file teste.php:

<form method="post" action="teste2.php" enctype="multipart/form-data">
  <textarea name="email" cols="110" rows="30" id="email"></textarea>
  <br />
  <input type="submit" value="Dividir" />
</form>

file teste2.php

<?php
    $str = $_POST['email'];
    $re = "/(?:\sF\d+.*?\n\n)(\n)/"; 
    $subst = ">>CUT HERE>>"; 
    $result = preg_replace($re, $subst, $str);
    echo $result;
?>

I try this here: https://3v4l.org/mqrJc and it works. It seems like a problem when I send the text in a textarea by post, Because If I use a string it works. Can anyone help me? Thank you very much in advance!

Try to add

echo $_POST['email'];

and see if it differs from what you expect

Hi,
I have tried that and the newlines disappear. It prints this:

A N K U N F T 11.08.15 *** NEUBUCHUNG *** 11.08.15 xxx xxx X3 2830 14:25 17:50 18.08.15 xxx xxx X3 2831 18:40 F882129 dsdsaidsaia F882129 xxxyxyagydaysd A N K U N F T 18.08.15 *** NEUBUCHUNG *** 11.08.15 xxx xxx X3 2830 14:25 17:50 18.08.15 xxx xxx X3 2831 18:40 F881554 ZXCXZCXCXZCCXZ F881554 xcvcxvcxvcvxc F881554 xvcxvcxcvxxvccvxxcv 11.08.15 xxx xxx X3 2830 14:25 17:50 18.08.15 xxx xxx X3 2831 18:40 F881605 xczxcdfsfdsdfs F881605 zxccxzxzdffdsfds

Which is exactly the same as the print of the result variable… I could substitute the newlines with br, but then the regex wouldnt work…

You should see line breaks in page source (press Ctrl+U)
In normal mode browser hides them

Correct, however why doesnt >>CUT HERE>> appear where it is suposed to? And works at: https://3v4l.org/mqrJc

Windows throws in a \r along with \n
try

$str = str_replace("\r", '', $str);

Now \n\n will match.

More than likely your email had \r\n\r\n at the end of those lines.

1 Like

Yes, thats right! I found this and changed the regex to:
(?:\sF\d+.*?\n\n)\K(\n)

Your solution also works!!
Thank you very much!!

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.