SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Enthusiast
    Join Date
    Aug 2000
    Posts
    37
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Character codes (converter script)

    I am hoping someone can help me here. I am trying to make a script that will take an email address and convert it to all character codes (‘ etc.) I have written the small script below (please keep in mind im new to php) and you will see the problem with it by looking at it. Is there any other way to search the string and replace each letter?

    The script:

    Code:
    <?
    
    // Do lower case
    $email=ereg_replace("a","&amp;#97;",$email);
    $email=ereg_replace("b","&amp;#98;",$email);
    $email=ereg_replace("c","&amp;#99;",$email);
    $email=ereg_replace("d","&amp;#100;",$email);
    $email=ereg_replace("e","&amp;#101;",$email);
    $email=ereg_replace("f","&amp;#102;",$email);
    $email=ereg_replace("g","&amp;#103;",$email);
    $email=ereg_replace("h","&amp;#104;",$email);
    $email=ereg_replace("i","&amp;#105;",$email);
    $email=ereg_replace("j","&amp;#106;",$email);
    $email=ereg_replace("k","&amp;#107;",$email);
    $email=ereg_replace("l","&amp;#108;",$email);
    $email=ereg_replace("m","&amp;#109;",$email);
    $email=ereg_replace("n","&amp;#110;",$email);
    $email=ereg_replace("o","&amp;#111;",$email);
    $email=ereg_replace("p","&amp;#112;",$email);
    $email=ereg_replace("q","&amp;#113;",$email);
    $email=ereg_replace("r","&amp;#114;",$email);
    $email=ereg_replace("s","&amp;#115;",$email);
    $email=ereg_replace("t","&amp;#116;",$email);
    $email=ereg_replace("u","&amp;#117;",$email);
    $email=ereg_replace("v","&amp;#118;",$email);
    $email=ereg_replace("w","&amp;#119;",$email);
    $email=ereg_replace("x","&amp;#120;",$email);
    $email=ereg_replace("y","&amp;#121;",$email);
    $email=ereg_replace("z","&amp;#122;",$email);
    
    // Do upper case
    $email=ereg_replace("A","&amp;#65;",$email);
    $email=ereg_replace("B","&amp;#66;",$email);
    $email=ereg_replace("C","&amp;#67;",$email);
    $email=ereg_replace("D","&amp;#68;",$email);
    $email=ereg_replace("E","&amp;#69;",$email);
    $email=ereg_replace("F","&amp;#70;",$email);
    $email=ereg_replace("G","&amp;#71;",$email);
    $email=ereg_replace("H","&amp;#72;",$email);
    $email=ereg_replace("I","&amp;#73;",$email);
    $email=ereg_replace("J","&amp;#74;",$email);
    $email=ereg_replace("K","&amp;#75;",$email);
    $email=ereg_replace("L","&amp;#76;",$email);
    $email=ereg_replace("M","&amp;#77;",$email);
    $email=ereg_replace("N","&amp;#78;",$email);
    $email=ereg_replace("O","&amp;#79;",$email);
    $email=ereg_replace("P","&amp;#80;",$email);
    $email=ereg_replace("Q","&amp;#81;",$email);
    $email=ereg_replace("R","&amp;#82;",$email);
    $email=ereg_replace("S","&amp;#83;",$email);
    $email=ereg_replace("T","&amp;#84;",$email);
    $email=ereg_replace("U","&amp;#85;",$email);
    $email=ereg_replace("V","&amp;#86;",$email);
    $email=ereg_replace("W","&amp;#87;",$email);
    $email=ereg_replace("X","&amp;#88;",$email);
    $email=ereg_replace("Y","&amp;#89;",$email);
    $email=ereg_replace("Z","&amp;#90;",$email);
    
    // Do Numbers
    $email=ereg_replace("0","&amp;#48;",$email);
    $email=ereg_replace("1","&amp;#49;",$email);
    $email=ereg_replace("2","&amp;#50;",$email);
    $email=ereg_replace("3","&amp;#51;",$email);
    $email=ereg_replace("4","&amp;#52;",$email);
    $email=ereg_replace("5","&amp;#53;",$email);
    $email=ereg_replace("6","&amp;#54;",$email);
    $email=ereg_replace("7","&amp;#55;",$email);
    $email=ereg_replace("8","&amp;#56;",$email);
    $email=ereg_replace("9","&amp;#57;",$email);
    
    // Do Misc Chars
    $email=ereg_replace("@","&amp;#64;",$email);
    $email=ereg_replace("\.","&amp;#46;",$email);
    $email=ereg_replace("_","&amp;#95;",$email);
    $email=ereg_replace("-","&amp;#45;",$email);
    $email=ereg_replace(" ","&amp;#160;",$email);
     
     
     echo $email;
     
    ?>
    Thanks
    ~Chris
    http://www.iguanaland.com

  2. #2
    SitePoint Evangelist mad-onion's Avatar
    Join Date
    Aug 2000
    Location
    Land of the long white cloud
    Posts
    556
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Personally i would use an array to accomplish this...

    Code:
    $codes=array("a"=>"&#97;", "b"=>"&#98;");
    //make $string equal the "string" you want to do the replacements in..
    $string="dsfndfknfd";
    
    //now we will loop through and do all the replacements..
    foreach($codes as $char=>$new){
      $string=str_replace($char, $new, $string);
    }
    That should work..of course you will need to add all the codes to the array!
    SiteOptions >> Services :: Products :: Contact
    Developers of PHP, C++, Visual Basic, MySQL, and more!

  3. #3
    SitePoint Enthusiast
    Join Date
    Aug 2000
    Posts
    37
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    OK here is what I have now .... The same thig is happening though. When the first "a" is replaced with "&amp;#97;" , then the next time is searches the string it finds that a in the word "amp" and replaces it too. Then it just seems to be stuck in that loop.

    http://www.glidetech.com/converter.phps

    I am calling it like this:
    http://www.glidetech.com/converter.p...l=john@doe.com

    Could i be doing something wrong?

    Thanks alot
    ~Chris

  4. #4
    SitePoint Evangelist mad-onion's Avatar
    Join Date
    Aug 2000
    Location
    Land of the long white cloud
    Posts
    556
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hey, arghh my code was very bad...sorry! I have written and am still testing the below...it appears to be working!

    Code:
    <?
    $codes=array(
        "a" => "&amp;#97;",
        "b" => "&amp;#98;",
        "c" => "&amp;#99;",
        "d" => "&amp;#100;",
        "e" => "&amp;#101;",
        "f" => "&amp;#102;",
        "g" => "&amp;#103;",
        "h" => "&amp;#104;",
        "i" => "&amp;#105;",
        "j" => "&amp;#106;",
        "k" => "&amp;#107;",
        "l" => "&amp;#108;",
        "m" => "&amp;#109;",
        "n" => "&amp;#110;",
        "o" => "&amp;#111;",
        "p" => "&amp;#112;",
        "q" => "&amp;#113;",
        "r" => "&amp;#114;",
        "s" => "&amp;#115;",
        "t" => "&amp;#116;",
        "u" => "&amp;#117;",
        "v" => "&amp;#118;",
        "w" => "&amp;#119;",
        "x" => "&amp;#120;",
        "y" => "&amp;#121;",
        "z" => "&amp;#122;",
        "A" => "&amp;#65;",
        "B" => "&amp;#66;",
        "C" => "&amp;#67;",
        "D" => "&amp;#68;",
        "E" => "&amp;#69;",
        "F" => "&amp;#70;",
        "G" => "&amp;#71;",
        "H" => "&amp;#72;",
        "I" => "&amp;#73;",
        "J" => "&amp;#74;",
        "K" => "&amp;#75;",
        "L" => "&amp;#76;",
        "M" => "&amp;#77;",
        "N" => "&amp;#78;",
        "O" => "&amp;#79;",
        "P" => "&amp;#80;",
        "Q" => "&amp;#81;",
        "R" => "&amp;#82;",
        "S" => "&amp;#83;",
        "T" => "&amp;#84;",
        "U" => "&amp;#85;",
        "V" => "&amp;#86;",
        "W" => "&amp;#87;",
        "X" => "&amp;#88;",
        "Y" => "&amp;#89;",
        "Z" => "&amp;#90;",
        "0" => "&amp;#48;",
        "1" => "&amp;#49;",
        "2" => "&amp;#50;",
        "3" => "&amp;#51;",
        "4" => "&amp;#52;",
        "5" => "&amp;#53;",
        "6" => "&amp;#54;",
        "7" => "&amp;#55;",
        "8" => "&amp;#56;",
        "9" => "&amp;#57;",
        "@" => "&amp;#64;",
        "." => "&amp;#46;",
        "_" => "&amp;#95;",
        "-" => "&amp;#45;",
        " " => "&amp;#160;",
    );
    
     //make $string equal the "string" you want to do the replacements in..
    $string=$email;
    
    //now we will loop through and do all the replacements..
      for($done=0; $done<strlen($string); $done++){
            $letter=substr($string, $done, 1);
            $letter=$codes[$letter];
            $all.=$letter;
      }
    
     echo $all;
    ?>
    SiteOptions >> Services :: Products :: Contact
    Developers of PHP, C++, Visual Basic, MySQL, and more!

  5. #5
    ********* Callithumpian silver trophy freakysid's Avatar
    Join Date
    Jun 2000
    Location
    Sydney, Australia
    Posts
    3,798
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    That might be a good way of going if you are being paid by lines of code However, there is a logic error in your solution in that you are replacing characters with numerals and then later replacing those numerals with other numerals. In any case, there is a simpler way to achieve your desired result.

    The function ord will return the ASCII code of a character (the ordinal value)
    http://php.net/manual/en/function.ord.php

    So you can iterate through a string (by treating it as an array) and pack a new string with the html code for each char. To pack it into HTML you would have to prefix each ASCII value with '&#' and suffix it with ';'.

    give this a whirl:
    PHP Code:
    <?php
    function htmlCharCode($str) { // returns string
       
    for ($i 0$i strlen($str); $i++) {
          
    $encodedStr .= '&#' ord($str[$i]) . ';';
       }
       return 
    $encodedStr;
    }

    // test
    $email 'doda@foobar.com';
    echo 
    htmlCharCode($email);
    ?>
    Last edited by freakysid; Jul 5, 2001 at 04:50.

  6. #6
    SitePoint Evangelist mad-onion's Avatar
    Join Date
    Aug 2000
    Location
    Land of the long white cloud
    Posts
    556
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP really does have a function for every occasion!
    SiteOptions >> Services :: Products :: Contact
    Developers of PHP, C++, Visual Basic, MySQL, and more!


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
  •