1

I am trying to use preg_replace to convert <br> or <br /> to a carriage return (&#13;). My problem seems to be that it either doesn't find the <br>'s or doesn't recognize the hex code I'm trying to pass in. Here is my relevant PHP:

preg_replace('`<br(?: /)?>(\x{13}/u)`', '$1', $content);

Other Information: The strings I am passing in have &quot; but I don't think that will interfere with preg_replace().

Here are a couple links that have helped me get this far:
http://www.webmasterworld.com/forum21/10973.htm (use carriage return instead of \n in tooltips)
http://us2.php.net/manual/en/reference.pcre.pattern.modifiers.php#58409 (use \x{13} instead of &#13)

hakre
  • 193,403
  • 52
  • 435
  • 836
Kevin
  • 600
  • 5
  • 19

2 Answers2

0

Hmmm, You seem to have preg_replace formatted a little strange. Not sure why your doing it with the backreferences like that? For something simple like this, the First variable should be the regex. 2nd should be replacement, and third should be subject:

preg_replace('%<br.*?>%', '&#13;', $content);

I think that will work, but I am not great at regexes. .*? should match any characters up to the next >

Ben
  • 745
  • 7
  • 23
  • 1
    The semi colon is inside a string so will not be interpreted as a line ending anyway. So you first implementation should run fine. – Paul.s Jul 07 '11 at 23:03
  • Thanks Ben! It's now finding and replacing my `
    `'s! The ` ` doesn't seem to be creating a carriage return in my tooltip but that's not a PHP question.
    – Kevin Jul 07 '11 at 23:12
  • @paul.s Can you expound on your comment? I tried using `[\x{13}/u]` but it just put that in as plaintext. – Kevin Jul 07 '11 at 23:17
  • Good, but just so you know, Paul's pattern example is a bit less greedy than mine. Mine will work if say you have
    or weird things like that. and if your
    is missing the closing tag (>) it will replace EVERYTHING up to the next closing tag it happens to find.
    – Ben Jul 07 '11 at 23:18
  • On your edit: I got an unepected end error where I closed my php script. – Kevin Jul 07 '11 at 23:26
  • Probably just use the one that worked then =) Carriage returns in title tags are finicky.. Try or Or go with a css or JS solution. See here: http://stackoverflow.com/questions/358874/how-can-i-use-a-carriage-return-in-a-html-tooltip – Ben Jul 07 '11 at 23:28
  • Thanks for the link! I checked in IE and ` ` is working fine! Firefox still has a few small things that are annoying and I'm adding this one to the list. ;) – Kevin Jul 07 '11 at 23:32
  • You should edit your answer with the final result. `preg_replace('%%', ' *', $key);` in case anyone ever has the same issue. Thanks again for the help! – Kevin Jul 08 '11 at 16:29
  • What exactly is the * after ?? – Ben Jul 08 '11 at 18:51
  • Sorry, I meant to add that you can optionally replace that * with a line break (comments don't allow block code) because manually inserting a line break will help some older browsers. – Kevin Jul 11 '11 at 18:16
0

Are you using this for the display to the web? If so I am unsure why you want to replace the <br /> tag for a carriage return. A reply from the first link you posted correctly states:

While inserting the entity for a carriage return/linefeed ( or ) will produce the desired result in Internet Explorer for Windows, it is not part of any standard and is not supported, for instance, by Gecko browsers on Windows. I have not tested other platforms.

@choster

However to correct your problem. The first argument is the pattern to match, the second argument is what you want to replace that match with and the third is the content.

The correct implementation would be something like this

echo preg_replace('/<br\s?\/?>/', '\x{13}/u', "<br> <br />");

I'm not sure if the \x{13}/u bit will work as I have not tested.

Paul.s
  • 38,494
  • 5
  • 70
  • 88
  • The reason is because I want to put the returned content into a tooltip which just outputs a plaintext `
    `. I should have been more specific in my original question - although I did include a link to the page about putting a line break in a tooltip.
    – Kevin Jul 07 '11 at 23:20
  • Did you read my answer with the quote form the page you listed? It is not part of any standard therefore there is a good chance it will not work anyway. I only carried on answering to show how your regex was malformed. Unfortunately I am not sure how you print a hex code in PHP – Paul.s Jul 07 '11 at 23:29
  • Paul, yes, thanks for the help. I'll have to survive without breaks in some browsers. Ben's solution works great in IE. As a standard evolves I'll have to update my function. Here is what I ended up with: `preg_replace('%%', ' *', $key);` with a line break where the * is. It works in Chrome as well. – Kevin Jul 07 '11 at 23:41