/website/source/php/strings/chr.html
HTML | 53 lines | 52 code | 1 blank | 0 comment | 0 complexity | 427f995e2da5ece922e738a5fccbd057 MD5 | raw file
- ---
- warning: 'This file is auto generated by `npm run web:inject`, do not edit by hand'
- examples:
- - |-
- chr(75) === 'K'
- chr(65536) === '\uD800\uDC00'
- estarget: es5
- returns:
- - |-
- true
- true
- dependencies: []
- authors:
- original by:
- - 'Kevin van Zonneveld (http://kvz.io)'
- improved by:
- - 'Brett Zamir (http://brett-zamir.me)'
- notes: []
- type: function
- layout: function
- title: PHP's chr in JavaScript
- description: >-
- Here’s what our current JavaScript equivalent to <a
- href="http://php.net/manual/en/function.chr.php">PHP's chr</a> looks like.
- function: chr
- category: strings
- language: php
- permalink: php/strings/chr/
- alias:
- - /functions/php/chr/
- - /functions/strings/chr/
- - /php/chr/
- - /functions/chr/
- ---
- {% codeblock lang:javascript %}module.exports = function chr (codePt) {
- // discuss at: http://locutus.io/php/chr/
- // original by: Kevin van Zonneveld (http://kvz.io)
- // improved by: Brett Zamir (http://brett-zamir.me)
- // example 1: chr(75) === 'K'
- // example 1: chr(65536) === '\uD800\uDC00'
- // returns 1: true
- // 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)
- }
- {% endcodeblock %}