Results 1 to 5 of 5

Thread: PHP Equivalent for a few Javascript functions

  1. #1

    Thread Starter
    New Member
    Join Date
    Feb 2011
    Posts
    5

    PHP Equivalent for a few Javascript functions

    Hi, I'm trying to find some PHP equivalents of a few Javascript functions... I just looked all over for them and cant find them, here are the functions:

    The fromCharCode() method converts Unicode values to characters.

    Note: This method is a static method of the String object. The syntax is always String.fromCharCode() and not string.fromCharCode().
    javascript Code:
    1. String.fromCharCode(n1, n2, ..., nX)





    The charCodeAt() method returns the Unicode of the character at the specified index in a string.

    The index of the first character is 0, and the index of the last character in a string called "txt", is txt.length-1.
    javascript Code:
    1. string.charCodeAt(index)


    All help is appreciated!

  2. #2
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    Re: PHP Equivalent for a few Javascript functions

    For the second one... what you need is the substr() function...
    http://us3.php.net/manual/en/function.substr.php

    php Code:
    1. theChar = substr(someString, 1);
    It too is also 0-index based...

    For the unicode conversion.... I'm not sure... try this: http://us3.php.net/manual/en/function.utf8-decode.php ... But I'm not convinced it's the right one, and I'm not finding much else that even comes close.

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  3. #3
    PowerPoster
    Join Date
    Sep 2003
    Location
    Edmonton, AB, Canada
    Posts
    2,629

    Re: PHP Equivalent for a few Javascript functions

    Look at ord(), substr(), and chr().

    PHP Code:
    // usage: echo charCodeAt("This is a string", 7)
    function charCodeAt($str$i){
      return 
    ord(substr($str$i1));
    }

    // usage: echo fromCharCode(72, 69, 76, 76, 79)
    function fromCharCode(){
      
    $output '';
      
    $chars func_get_args();
      foreach(
    $chars as $char){
        
    $output .= chr((int) $char);
      }
      return 
    $output;

    do those do what you're looking for?
    Like Archer? Check out some Sterling Archer quotes.

  4. #4

    Thread Starter
    New Member
    Join Date
    Feb 2011
    Posts
    5

    Re: PHP Equivalent for a few Javascript functions

    Thats exactly it! Thank you. I knew PHP.net had those functions, I just couldnt find them. Putting them into nice little functions makes it even better.

  5. #5
    Hyperactive Member BramVandenbon's Avatar
    Join Date
    Jan 2002
    Location
    Belgium
    Posts
    502

    Re: PHP Equivalent for a few Javascript functions

    The charCodeAt() method returns the Unicode of the character at the specified index in a string...
    In that case I would like to warn you though that the ord() function of php does not convert unicode. It can only handle ASCII.

    The following unofficial code probably also does the trick for unicode (but haven't tested it yet / didn't write it myself).
    Code:
    function uniord($c) {
        $h = ord($c{0});
        if ($h <= 0x7F) {
            return $h;
        } else if ($h < 0xC2) {
            return false;
        } else if ($h <= 0xDF) {
            return ($h & 0x1F) << 6 | (ord($c{1}) & 0x3F);
        } else if ($h <= 0xEF) {
            return ($h & 0x0F) << 12 | (ord($c{1}) & 0x3F) << 6
                                     | (ord($c{2}) & 0x3F);
        } else if ($h <= 0xF4) {
            return ($h & 0x0F) << 18 | (ord($c{1}) & 0x3F) << 12
                                     | (ord($c{2}) & 0x3F) << 6
                                     | (ord($c{3}) & 0x3F);
        } else {
            return false;
        }
    }
    Last edited by BramVandenbon; Feb 3rd, 2011 at 09:37 AM.
    ____________________________________________

    Please rate my messages. Thank you!
    ____________________________________________
    Bram Vandenbon
    http://www.bramvandenbon.com

Posting Permissions

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



Click Here to Expand Forum to Full Width