Results 1 to 7 of 7

Thread: Converting symbols with PHP

  1. #1
    Join Date
    May 2007
    Location
    England, UK
    Posts
    235
    Thanks
    3
    Thanked 6 Times in 6 Posts

    Default Converting symbols with PHP

    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.
    & = &
    ® = ®

  2. #2
    Join Date
    Jul 2006
    Location
    just north of Boston, MA
    Posts
    1,806
    Thanks
    13
    Thanked 72 Times in 72 Posts

    Default

    PHP Code:
    htmlentities($tring); 
    http://us3.php.net/htmlentities

  3. #3
    Join Date
    May 2007
    Location
    England, UK
    Posts
    235
    Thanks
    3
    Thanked 6 Times in 6 Posts

    Default

    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 &amp; some more text here <br>
    some text here &amp; some more text here &reg; <br><br>
    some text here &amp; some more text here &reg;
    Is this possible?

  4. #4
    Join Date
    Jan 2007
    Location
    Charlotte, NC
    Posts
    82
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default

    yeah, just include that &amp; in your database. If your database is reading the <br/>'s back as html line breaks, what makes you think it won't read &amp; as the & symbol?

  5. #5
    Join Date
    Jul 2006
    Location
    just north of Boston, MA
    Posts
    1,806
    Thanks
    13
    Thanked 72 Times in 72 Posts

    Default

    htmlspecialchars($tring);

    is the other method to which parse character codes.

    yeah, just include that &amp; in your database. If your database is reading the <br/>'s back as html line breaks, what makes you think it won't read &amp; 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 }

  6. #6
    Join Date
    May 2006
    Location
    Sydney, Australia - Near the coast.
    Posts
    1,995
    Thanks
    0
    Thanked 8 Times in 7 Posts

    Default

    It's better to use
    Code:
    <p>some text</p><p>some text</p>
    Peter - alotofstuffhere[dot]com - Email Me - Donate via PayPal - Got spare hardware? Donate 'em to me :) Just send me a PM.
    Currently: enjoying the early holidays :)
    Read before posting: FAQ | What you CAN'T do with JavaScript | Form Rules | Thread Title Naming Guide

  7. #7
    Join Date
    May 2007
    Location
    England, UK
    Posts
    235
    Thanks
    3
    Thanked 6 Times in 6 Posts

    Default

    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.

    PHP Code:
    $text_from_database "this text was generated by a mysql query <br> & contains some symbols®"

    $replace_this = array("®""&");
    $replace_with = array("&reg;""&amp;");

    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> &amp; contains some symbols&reg;

    Which would then display like this in the browser:

    this text was generated by a mysql query
    & contains some symbols®

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •