SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Guru Dashman's Avatar
    Join Date
    Jan 2006
    Location
    Manchester, UK
    Posts
    627
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Convert text to HTML special characters

    Hey guys....

    Is it possible to write a script/function that could convert text to its HTML special characters equivalent. I know that I can just use html_specialchars , but I would also like to be able to convert alphanumeric characters.

    Is it possible to do a complete conversion like this? Or even better, is there a PHP function that I could use?

    Ironically, the characters get processed when I preview this post so I have uploaded an example here so you can view the source code to see what I mean.

    Cheers!

    Dave

  2. #2
    ✯✯✯ silver trophybronze trophy php_daemon's Avatar
    Join Date
    Mar 2006
    Posts
    5,284
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    I don't remeber if there's a function for that, probably there is no, so here's a custom one:
    PHP Code:
    echo encode("me@me.com");

    function 
    encode($string){
      
    $count=strlen($string);
      
    $encoded="";
      for(
    $i=0;$i<$count;$i++){
        
    $encoded.="&#".ord($string[$i]).";";
      }
      return 
    $encoded;

    Saul

  3. #3
    SitePoint Guru Dashman's Avatar
    Join Date
    Jan 2006
    Location
    Manchester, UK
    Posts
    627
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi....

    Thats brilliant, works like a charm, thanks very much for the code.

    D

  4. #4
    ✯✯✯ silver trophybronze trophy php_daemon's Avatar
    Join Date
    Mar 2006
    Posts
    5,284
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    You're very welcome, Dave
    Saul

  5. #5
    SitePoint Wizard stereofrog's Avatar
    Join Date
    Apr 2004
    Location
    germany
    Posts
    4,324
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

  6. #6
    ✯✯✯ silver trophybronze trophy php_daemon's Avatar
    Join Date
    Mar 2006
    Posts
    5,284
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by stereofrog
    That won't convert all characters, will it?
    Saul


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
  •