Xeoncross Posted June 21, 2009 Share Posted June 21, 2009 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. Quote Link to comment Share on other sites More sharing options...
Xeoncross Posted June 21, 2009 Author Share Posted June 21, 2009 The PHP markdown reports a regex almost like this which also works in regexpal. (^>.+\n(.+\n)*)+ However, like the other regex's above it either selects nothing, or with the "m" modifier selects almost everything. Quote Link to comment Share on other sites More sharing options...
nrg_alpha Posted June 21, 2009 Share Posted June 21, 2009 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 ) Quote Link to comment Share on other sites More sharing options...
Xeoncross Posted June 21, 2009 Author Share Posted June 21, 2009 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) Quote Link to comment Share on other sites More sharing options...
Xeoncross Posted June 21, 2009 Author Share Posted June 21, 2009 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. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.