PDA

View Full Version : Help with preg_replace and regexp


makaiguy
19 Aug 2006, 03:13
Working on a small PHP mailer form project on Apache v1.3.36 to sent html-formatted mail.

Form is POSTed with message text in a textarea named COMMENT. Some code I'm adapting uses the following to strip linefeeds and replace with html breaks:


Code:
---------------
Code is only visible to licensed users, and only when logged into the forums.
---------------



Only problem is that any apostrophes in the submitted message get a \ inserted before them. Thus "it's" becomes "it\'s" in the received mail.

I don't know beans about regular expressions. Can anybody help straighten this out?

Or is there something else that needs to be done to have apostrophes show up correctly?

harmor19
19 Aug 2006, 03:30
Below that line add



Code:
---------------
Code is only visible to licensed users, and only when logged into the forums.
---------------

Code Monkey
19 Aug 2006, 03:35
Why don't you just use the php function nl2br();

nl2br(string string) //Inserts HTML line breaks before all newlines in a string

makaiguy
19 Aug 2006, 03:48
Below that line add



Code:
---------------
Code is only visible to licensed users, and only when logged into the forums.
---------------



Works wonderfully, thanks! And much to my surprise it even leaves slashes originally entered as part of the text intact.

Why don't you just use the php function nl2br();

nl2br(string string) //Inserts HTML line breaks before all newlines in a string

Thanks for this. It's certainly easier for me to understand the coding without having to deal with the regex.

But still getting the \' , so still have to use stripslashes().

Any idea which of the two approaches is less server intensive?

Code Monkey
19 Aug 2006, 05:13
well, nl2br was made to do that and only that, so I would guess that is the best method.