Jump to content

Selecting Email styled blockquotes


Xeoncross

Recommended Posts

Markdown is one of the few markup tools that selects ">blah, blah..." styled old-fashioned email quotes and converts it into HTML blockquotes.

 

I have been trying to do that with PHP but I just can't get it together. So far I have lots of regex patterns that work in regexpal - but not PHP. And I can't figure out why. I have used the /.../m and other modifiers yet it won't cache the string right.

 

(^>([^\n]+(\n|$)+?)+?)
^>(([^\n]+\n)+?)
^\>((.|\n)+?)\n\n

 

<this is some text >followed by an invalid quote

>this is a real quote

> this is a quote
that is broken up
across lines


>Another quote
>with a starting
>> sign

Just plain text
>End quote

 

 

The goal would be to catch all lines that start with a ">" followed by EVERYTHING upto two returns "\n\n". That should be a standard quote.

 

Link to comment
Share on other sites

If I understand correctly, you mean something like this?

 

example:

$str = <<<EOD
<this is some text >followed by an invalid quote

>this is a real quote

> this is a quote
that is broken up
across lines


>Another quote
>with a starting
>> sign

Just plain text
>End quote
EOD;

preg_match_all('#^(>[^>]+)#m', $str, $matches);
$matches[1] = array_map('trim', $matches[1]);
echo '<pre>'.print_r($matches[1],true);

 

Output:

Array
(
     => >this is a real quote[1] => > this is a quote
that is broken up
across lines
    [2] => >Another quote
    [3] => >with a starting
    [4] => >End quote
)

Link to comment
Share on other sites

Thanks, that is along the same idea. But I don't want each new line starting with a ">" to be a new blockquote. If you look back at the old emails there were often many lines in replies / forwards that started with a ">". So I want all of those (up to two line brakes) to be counted as a single blockquote. With your code I would have a new blockquote for each line which won't work.

 

Thanks Joe

>Joe wrote:
>Sent you your check
>-Joe

 

 

To allow laziness, I also want to just look for something that starts with a ">" and goes to two line brakes (\n\n) even if those other lines don't start with a ">".

 

Thanks Joe

>Joe wrote:
Sent you your check
-Joe

-Sam (outside of the blockquote)

Link to comment
Share on other sites

Problem solved. I am on a windows box and so since the text I was reading from was in a text file it had the windows "\r\n" instead of "\n". It's the simple things that get you.

 

$text = str_replace("\r\n", "\n", $text);

 

So all those codes do work.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.