Break Row to Carriage Return preg_replace
I am trying to use preg_replace to convert <br>
or <br />
to a carriage return ( )
. 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. Her开发者_StackOverflowe is my relevant PHP:
preg_replace('`<br(?: /)?>(\x{13}/u)`', '$1', $content);
Other Information: The strings I am passing in have "
but I don't think that will interfere with preg_replace()
.
\n
in tooltips)
http://us2.php.net/manual/en/reference.pcre.pattern.modifiers.php#58409 (use \x{13}
instead of 
)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.*?>%', ' ', $content);
I think that will work, but I am not great at regexes. .*? should match any characters up to the next >
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.
精彩评论