|
-
Feb 2nd, 2011, 02:42 PM
#1
Thread Starter
New Member
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:
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.
All help is appreciated!
-
Feb 2nd, 2011, 04:11 PM
#2
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:
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
-
Feb 2nd, 2011, 04:16 PM
#3
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, $i, 1)); }
// 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?
-
Feb 2nd, 2011, 07:46 PM
#4
Thread Starter
New Member
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.
-
Feb 3rd, 2011, 09:33 AM
#5
Hyperactive Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|