1

So I want to turn the line after /r completely red, but only this line.

So far I've got this (this only works because \n gets replaced by <br> earlier; it doesn't matter at the moment):

$str = preg_replace("/\/r(.*)<br>/", '<font color="red">$1</font><br>', $str);

This works so far, but only if there is really a line break. If you just type one line, it won't make it red.

Is there any way to get the end of a line, or the end of the whole text?

Amal Murali
  • 75,622
  • 18
  • 128
  • 150
nn3112337
  • 459
  • 6
  • 21
  • 5
    Is that, oh yeah HTML parsing with reg ex http://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags – Valentin Mercier Jul 28 '14 at 14:28
  • 2
    @ValentinMercier: Did you read the complete question? Or just assumed that the question is about parsing HTML as you saw a `
    ` in it? :)
    – Amal Murali Jul 28 '14 at 14:30
  • Please include some example HTML with the input, otherwise there's no way for us to test the problem. – Mr. Llama Jul 28 '14 at 14:33

1 Answers1

2

I think you mean to make <br> optional for last line. You can use:

$str = preg_replace('~/r(.*?)(?:<br>|$)~uis', '<font color="red">$1</font><br>', $str);
anubhava
  • 761,203
  • 64
  • 569
  • 643
  • 1
    If you want to do this you should probably add `uis` as flags of your regexp (unicode entities, ignore case, single-line mode) – Brandin Jul 28 '14 at 14:39