There’s some text in my db with quotes in it. In their raw form they look like this:
>Some quote <br /> Some simple text<br /> <br> > another quote with random <b>tags</b> and <a href=>links</a> in it
What I wanted to do is make those quotes look somewhat distinctive from other text – either in php part before they go to the database or when they’re displayed on the page (doesn’t matter much to me, whichever would work).
Problem is there’s little consistency: there might be extra spaces, other closing tags, etc. Might be a few per string, in a row or not. The only thing that’s the same is >
(not >
– it’s how it saved in db) in the beginning and end of line OR end of string at the end of a quote.
Closest thing I could manage myself is below, and it covers for some quotes – but not those that are at the very end, and sometimes it gets interrupted with random tags.
preg_replace("/>([^*]*)<br/", "<mark class='quote'>$1</mark><br", $string);
Any way to fix it? Thanks in advance!
Advertisement
Answer
You need to make the quantifier not greedy with a ?
, and look for <br
or the end of the string by using an alteration.
>([^*]*?)(<br|$)
replacement should be <mark class='quote'>$1</mark>$2
so you put the <br
or end line back.