View Full Version : Converting symbols with PHP
jc_gmk
11-15-2007, 02:47 PM
I have a MySQL database that has lots of text in.
some of the characters used are things like &, ®
How can I make sure that when a php script calls the data it converts all the characters in to the correct html e.g.
& = &
® = ®
boogyman
11-15-2007, 02:51 PM
htmlentities($tring);
http://us3.php.net/htmlentities
jc_gmk
11-15-2007, 03:05 PM
Had tried that but it didn't work. Just realized why not though.
here is an example of the text in my database:
some text here & some more text here <br>
some text here & some more text here ® <br><br>
some text here & some more text here ®
I want to include the <br>'s as line breaks but change the charachters into html.
I want it to output html like this
some text here & some more text here <br>
some text here & some more text here ® <br><br>
some text here & some more text here ®
Is this possible?
jamiller
11-15-2007, 03:30 PM
yeah, just include that & in your database. If your database is reading the <br/>'s back as html line breaks, what makes you think it won't read & as the & symbol? ;)
boogyman
11-15-2007, 04:04 PM
htmlspecialchars($tring);
is the other method to which parse character codes.
yeah, just include that & in your database. If your database is reading the <br/>'s back as html line breaks, what makes you think it won't read & as the & symbol?
some characters are harmful to the database, some characters are harmful to the web browser. its better to just do the distinction when the time comes...
eg (%) is a quantifier in a SQL Database, however (%) is perfectly acceptable in a web browser. translation should always be done where appropriate if possible.
some text here & some more text here <br>
some text here & some more text here ® <br><br>
some text here & some more text here ®
this is very fishy as a possible misuse of the break line tag.
perhaps use <span>text</span> and in your stylesheet put span { display:block }
tech_support
11-16-2007, 09:09 AM
It's better to use
<p>some text</p><p>some text</p>
jc_gmk
11-16-2007, 12:26 PM
Thanks for all you help!
I have now sorted the problem.
The problem was that I already have hundreds of entries in my database similar to this, and would take hours to edit them all:
some text here & some more text here <br> something else could go here <br>
some text here & some more text here® <br><br>
some text® here & some more text here my solution was the str_replace() function e.g.
$text_from_database = "this text was generated by a mysql query <br> & contains some symbols®";
$replace_this = array("®", "&");
$replace_with = array("®", "&");
print str_replace($replace_this, $replace_with, $text_from_database);
This then would output this HTML:
this text was generated by a mysql query <br> & contains some symbols®
Which would then display like this in the browser:
this text was generated by a mysql query
& contains some symbols®
Powered by vBulletin® Version 4.2.2 Copyright © 2021 vBulletin Solutions, Inc. All rights reserved.