note 37806 added to function.str-replace
| From: | Daniellus83 at rn2 dot php dot net | Date: | Thu, 27 Nov 2003 21:49:50 +0000 |
| Subject: | note 37806 added to function.str-replace | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-61050@lists.php.net to get a copy of this message | ||
[ Common Problem with converting text from MySQL database to HTML: line breaks!! (Once and for all
the ULTIMATE SOLUTION ;) ]
Dear users....
I have struggled for many hours on the following problem wich many people would recognize:
1) you have text in an MySQL database
2) you have a PHP script
3a) the script inserts the text from the database into the HTML page
OR
3b) inserts the text into a javascript variable ..='your text'!
OR
3c) inserts into a textbox (or whatever...)
THE PROBLEM:
because php inserts directly from MySQL everything is inserted including ALL characters (like ENTER
' " ... etcetc) wich causes the HTML browser to fail to generate properly.
-------------------------------------
I red many text-piece all over the internet on how to convert with str_replace (par example) the
ENTER
[ n or nr or char(13) or whatever... Mac is different than Win or Linux... aaarrggghhhh!!]
But here the allways working (multi-platform) solution!...
thaadaaa:
A) MySQL database
1) Create an extra table in your MySQL database with 3 columns.
2) Put in the 1rst col : ID (=1,2,3,...)
3) Put in the 2nd col: the wrong character
(so if its an enter, put an enter in the cell!!)
4) Put in the 3rd col: the correct <HTML> character:
(so if it's an enter; put the HTML-one in it: <br>!!)
B) The script:
1) Take care that the string (with wrong characters) is loaded in $yourstring
2) Now create a while loop, the same as you would reading a whole table, but now you read the whole
(just made) character-table
3) read in the loop from the corresponding row from the table, the $wrong and the $correct
character.
4) put in the loop the code:
$corrected_yourstring = str_replace($wrong,$correct,$yourstring);
This should replace all evil-evil-evil characters ;)
(even ENTERs, wich are different on other platforms!, are replaced with in my case <br>)
PLEASE let me know what you think about this solution!
(dstrommel at hotmail.com),
Daniël
----
Manual Page -- http://www.php.net/manual/en/function.str-replace.php
Edit -- http://master.php.net/manage/user-notes.php?action=edit+37806
Delete -- http://master.php.net/manage/user-notes.php?action=delete+37806&report=yes
Reject -- http://master.php.net/manage/user-notes.php?action=reject+37806&report=yes
Search -- http://master.php.net/manage/user-notes.php