Use PHP functions in JavaScript

JavaScript chr

Converts a codepoint number to a character

1
2
3
4
56
7
8
9
1011
12
13
14
1516
17
18
19
20
function chr (codePt) {
    // Converts a codepoint number to a character  
    // 
    // version: 1109.2015
    // discuss at: http://phpjs.org/functions/chr    // +   original by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +   improved by: Brett Zamir (http://brett-zamir.me)
    // *     example 1: chr(75);
    // *     returns 1: 'K'
    // *     example 1: chr(65536) === '\uD800\uDC00';    // *     returns 1: true
    if (codePt > 0xFFFF) { // Create a four-byte string (length 2) since this code point is high
        //   enough for the UTF-16 encoding (JavaScript internal use), to
        //   require representation with two surrogates (reserved non-characters
        //   used for building other characters; the first is "high" and the next "low")        codePt -= 0x10000;
        return String.fromCharCode(0xD800 + (codePt >> 10), 0xDC00 + (codePt & 0x3FF));
    }
    return String.fromCharCode(codePt);
}
external links: original PHP docs | raw js source

Examples

Running

1
2
chr(75);
chr(65536) === '\uD800\uDC00';

Should return

1
true

Dependencies

No dependencies, you can use this function standalone.

Open syntax issues

php.js uses JsLint to help us keep our code consistent and prevent some common bugs.

Eventually we want all code to pass or at least take into consideration most fixes suggested by JsLint, following this JsLint configuration we’ve decided on.


Authors

Thanks to the following developers, you get to have chr goodness in JavaScript.

Comments

Add Comment
Use:
[CODE]
your_stuff('here');
[/CODE]
for proper code formatting
By submitting code here you are allowing us to use it in php.js hence dual licensing it under the MIT and GPL licenses

Gravatar
Kevin van Zonneveld
8 Jul '09 Permalink

q  @ Brett Zamir: Mediawiki it is! I installed a basic version in /wiki. Will link to it from the navigation once we have some goodness to show for it.

Gravatar
Brett Zamir
4 Jul '09 Permalink

q  I'm quite partial to Mediawiki. Well-tested, familiar syntax (at least among wikis), easy to install, extensible, well-supported by the community, etc. See http://www.mediawiki.org/wiki/Download and links on the page for 'how-to' articles such as customizing the logo, etc. (e.g., http://www.mediawiki.org/wiki/Manual:Skins and http://www.mediawiki.org/wiki/Manual:Gallery_of_user_styles )

Gravatar
Kevin van Zonneveld
3 Jul '09 Permalink

q  @ Brett Zamir: Ok i'm definitely cool with that. Just need to figure out what wiki to use and how to nicely integrate it into this site (cakephp). any suggestions are welcome

Gravatar
Brett Zamir
30 Jun '09 Permalink

q  Nice one, thanks Roland. Used the same approach in my LGPL Firefox Unicode chart view extension - https://addons.mozilla.org/en-US/firefox/addon/5235 (I should make an HTML 5 version of that one if somebody else doesn't, now that SQL can be used client-side to store character descriptions.)

Anyhow, Kevin, what do you think of a public wiki for demos of code like this one? We keep getting asked for demos, so maybe our users would like a little workspace for stuff like this (or derivatives of the functions, etc.). Wouldn't replace the compiler, and central review is still helpful, but we might see others be able to help out a little with introduction, offering special tricks, etc. What do you think?

Gravatar
Roland Hentschel
30 Jun '09 Permalink

q  just a simple way to use this script:

function charmap(font,size) {
	if (font==null) font="Arial";
	if (size==null) size=24;
	document.write("<style>\n"+
	"* { font:normal bold "+size+"pt "+font+"; }\n"+
	"td { text-align:center; }\n"+
	"</style>\n");
	document.write("<table>\n");
	for (x=0;x<16;x++) {
		document.write("<tr>\n");
		for (y=0;y<16;y++) {
			document.write("<td>"+chr(16*x+y)+"</td>\n");
		}
		document.write("</tr>\n");
	}
	document.write("</table>\n");
}

Gravatar
Kevin van Zonneveld
15 Mar '08 Permalink

q  @ fumbling fellow: Yeah I recently moved the css to a separate file, but I messed up apparently. Thank you, fixed it now!

Gravatar
fumbling fellow
15 Mar '08 Permalink

q  Looks like the code is working, the css file is not found so the cells are not changing color.
http://kevin.vanzonneveld.net/css/pj_tester.css

I found the function I was looking for though.
print_r

Gravatar
fumbling fellow
14 Mar '08 Permalink

q  Thanks for all the hard work!
This will be very useful to me.

I can't get your test page to work.
Seems like I had green cells a couple of weeks ago, but now nothing is happening :-(

Gravatar
Kevin van Zonneveld
3 Jan '08 Permalink

q  @ Kris: Thanks Kris that's very useful!

Gravatar
Kris Brixon
2 Jan '08 Permalink

q  Here are a few links to a similar effort from ColdFusion. You may be able to pick up a few functions they wrote.

http://cfjs.riaforge.org/
http://www.shlomygantz.com/customtags/CFMLjsLibrary/CfmljsLibrary_beta.htm


Contribute a New function