Closed Thread Icon

Preserved Topic: preg_replace for "\n"? (Page 1 of 1) Pages that link to <a href="https://ozoneasylum.com/backlink?for=12356" title="Pages that link to Preserved Topic: preg_replace for &amp;quot;\n&amp;quot;? (Page 1 of 1)" rel="nofollow" >Preserved Topic: preg_replace for &quot;\n&quot;? <span class="small">(Page 1 of 1)</span>\

 
Pugzly
Paranoid (IV) Inmate

From: 127.0.0.1
Insane since: Apr 2000

posted posted 08-08-2002 08:41

I enter data via a form into a MySQL database (via PHP). One of the form fields is a textarea. I need to convert line breaks/carriage returns to "=0D=0A". An example would be

"Hello, this

is

my text"

would be converted to

"Hello, this=0D=0Ais=0D=0Amy text"

but I'm having a hard time. I've tried

$comments = preg_replace("\n", "=0D=0A", $comments);

but that get's me an "Empty regular expression" error. What's the best method, and should I replace the text BEFORE writing it to the database, or just when reading/displaying it? Eventually, I'm going to need to replace the "=0D=0A" with the line breaks when I present the data back to the user for editing. But that's later.....

PS - And it wasn't my call to use "=0D=0A" - it's part of the vCard standard ( http://www.ietf.org/rfc/rfc2426.txt )




[This message has been edited by Pugzly (edited 08-08-2002).]

lallous
Paranoid (IV) Inmate

From: Lebanon
Insane since: May 2001

posted posted 08-08-2002 09:16

preg_replace("/\n/m", "=0D=0A", $comments);

you are not adding the modifiers to your expression.

mr.maX
Maniac (V) Mad Scientist

From: Belgrade, Serbia
Insane since: Sep 2000

posted posted 08-08-2002 09:20

Pugzly, I suggest that you use str_replace() function insetad of preg_replace() for such simple search/replace operation (preg_replace() is really an overkill in this case)...

$comments = str_replace("\n", "=0D=0A", $comments);

Also, you should perform the replace before adding data to the database.


Pugzly
Paranoid (IV) Inmate

From: 127.0.0.1
Insane since: Apr 2000

posted posted 08-08-2002 18:29

Ah....str_replace()

That worked great.

Thanks!

« BackwardsOnwards »

Show Forum Drop Down Menu