Reputation: 3105
I have a PHP script that deals with emails, I'd like to catch strings like this one:
>> John replied with a nice hi...
>> then added a second hi...
>
>> Ray said hello
Note the 3rd line contains only >
instead of >>
and normalise it
, ie. transform it into:
>> John replied with a nice hi...
>> then added a second hi...
>>
>> Ray said hello
In other words, I'd like to catch non-empty lines which starts with >
& replace them with >>
My question may look worthless, but I have a nice regexp that takes these >
& format them nicely, ie.
>> John replied with a nice hi...
>> then added a second hi...
>>
>> Ray said hello
becomes
[quote]
John replied with a nice hi...
then added a second hi...
Ray said hello
[/quote]
The scenario above breaks my formatting & makes the final output looks like this:
[quote_level_1]
John replied with a nice hi...
then added a second hi...
[/quote_level_1]
[quote_level_2]
[/quote_level_2]
[quote_level_1]
Ray said hello
[/quote_level_1]
My question is Is there a regexp I can use to handle this situation? I tried many regular expressions, none of them gave me correct results :(
I'm using PHP 5.4
I'm afraid I didn't formulate my question accurately (sorry about that), what I want is to catch non-empty lines which starts with >
& replace them with >>
, but only if the line in question is located between 2 non-empty lines which starts with >>
I hope I clarified my question, sorry!
Upvotes: 1
Views: 465
Reputation: 8052
http://php.net/manual/en/function.preg-replace.php
This seems to be a straightforward regexp application.
$normalized_text = preg_replace("/^>(.+)/m", ">>${1}", $your_text);
Upvotes: 0
Reputation: 59699
I'd like to catch non-empty lines which starts with > and replace them with >>
That regex looks like this:
/^>(?=[^>])/m
Where the /
is the delimiter, ^
matches the beginning of the line (because we specified multi-line mode with the m
modifier), and then specify that we must match something that isn't a >
after the first >
.
You would use it in PHP like this:
preg_replace( '/^>(?=[^>])/m', '>>', $str)
And you can see from this demo that this prints:
>> John replied with a nice hi...
>> then added a second hi...
>>
>> Ray said hello
Upvotes: 2