Search code examples
phpparsingpreg-match-allmultiple-matches

preg_match_all multiple matches and pattern for text between html text


I am using:

 preg_match_all("/(.*):\s(.*)\n/i", $email, $matches);

to get any 'From:' or 'subject:' etc. Which is all good except that my message doesn't come in as "MESSAGE:". it comes in the $matches array as [1][18] as below:

< HTML>
    < HEAD></ HEAD>
    < BODY style="background-color:#ffffff">
        **MY MESSAGE IS HERE**< br>

Therefore I need a preg_match_all that will : 1. give me all the from, subject or what have you (which i can keep the current pattern unless you know a better one). 2. Give me the text content that is in between the html tags. ** Is there anyway I can do this using just one preg_match_all which uses both pattern searches at once?**


Solution

  • If message reliably looks like that every time, just strip_tags($message); or remove the < HTML>< HEAD>< BODY style="background-color:#ffffff"> and < br>. with str_replace

    It should be noted that this is not necessarily a good idea, especially if the html at the beginning and end change.

    edit: slightly more elegant if your message is the same every time;

    $message = substr($message, 54);
    $message = substr($message, 0, strpos($message, "< br>---"));
    
    var_dump($message);