Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8328613
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T01:30:02+00:00 2026-06-09T01:30:02+00:00

Is it possible to shorten this code using preg_replace instead of preg_match? I am

  • 0

Is it possible to shorten this code using preg_replace instead of preg_match?

I am using it to remove quoted text from an email body.
Quoted text being when you quote somebody when you reply to an email.

# Get rid of any quoted text in the email body
# stripSignature removes signatures from the email
# $body is the body of an email (All headers removed)
$body_array = explode("\n", $this->stripSignature($body));
$new_body = "";
foreach($body_array as $key => $value)
{
    # Remove hotmail sig
    if($value == "_________________________________________________________________")
    {
        break;

    # Original message quote
    }
    elseif(preg_match("/^-*(.*)Original Message(.*)-*/i",$value,$matches))
    {
        break;

    # Check for date wrote string
    }
    elseif(preg_match("/^On(.*)wrote:(.*)/i",$value,$matches))
    {
        break;

    # Check for From Name email section
    }
    elseif(preg_match("/^On(.*)$fromName(.*)/i",$value,$matches))
    {
        break;

    # Check for To Name email section
    }
    elseif(preg_match("/^On(.*)$toName(.*)/i",$value,$matches))
    {
        break;

    # Check for To Email email section
    }
    elseif(preg_match("/^(.*)$toEmail(.*)wrote:(.*)/i",$value,$matches))
    {
        break;

    # Check for From Email email section
    }
    elseif(preg_match("/^(.*)$fromEmail(.*)wrote:(.*)/i",$value,$matches))
    {
        break;

    # Check for quoted ">" section
    }
    elseif(preg_match("/^>(.*)/i",$value,$matches))
    {
        break;

    # Check for date wrote string with dashes
    }
    elseif(preg_match("/^---(.*)On(.*)wrote:(.*)/i",$value,$matches))
    {
        break;

    # Add line to body
    }
    else {
        $new_body .= "$value\n";
    }
}

This almost works, but it keeps the first line “On Mon, Jul 30, 2012 at 10:54 PM, Persons Name wrote:”

$body = preg_replace('/(^\w.+:\n)?(^>.*(\n|$))+/mi', "", $body);
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T01:30:04+00:00Added an answer on June 9, 2026 at 1:30 am

    There’s probably a more elegant way of doing this, but this should work (assuming the regexps are correct):

    $search = array(
      "/^-*.*Original Message.*-*/i",
      "/^On.*wrote:.*/i",
      "/^On.*$fromName.*/i",
      "/^On.*$toName.*/i",
      "/^.*$toEmail.*wrote:.*/i",
      "/^.*$fromEmail.*wrote:.*/i",
      "/^>.*/i",
      "/^---.*On.*wrote:.*/i"
    );
    
    
    $body_array = explode("\n", $this->stripSignature($body));
    $body = implode("\n", array_filter(preg_replace($search, '', $body_array)));
    
    // or just
    
    $body = str_replace(
      array("\0\n", "\n\0"), '', preg_replace($search, "\0", $body)
    );
    

    Edit: As inhan pointed out, the dots in the email addresses (and potentially other special characters) need to be escaped using preg_quote() prior to being inserted into the patterns.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is this possible, or am I just trying to way overly shorten my code?
Is it possible to somehow shorten this code: var i=GetStringFromServer('/url'); if(i){ $('#Div1').hide(); $('#Div2').show(); }
I have a cluttery piece of code that I would like to shorten using
Is it possible to shorten a URL using the Goo.gl shortening api with a
I'm using this code to show messages: Common.getHandler().post(new Runnable(){ public void run(){ Toast.makeText(Common.getContext(), Text...,
Possible Duplicate: Shorten String in PHP (full words only) Below substr which iam using
Is it possible to do something like this (I use initializer blocks to shorten
How can I make this code shorter? For example with a foreach. if($Email ==
Possible Duplicate: How do I make a request using HTTP basic authentication with PHP
Possible Duplicate: && operator in Javascript In the sample code of the ExtJS web

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.