PHP's ctype_alnum in TypeScript

✓ Verified: PHP 8.3
Examples tested against actual runtime. CI re-verifies continuously. Only documented examples are tested.
Rosetta Stone: c/isalnum

How to use

Install via yarn add locutus and import: import { ctype_alnum } from 'locutus/php/ctype/ctype_alnum'.

Or with CommonJS: const { ctype_alnum } = require('locutus/php/ctype/ctype_alnum')

Use a bundler that supports tree-shaking so you only ship the functions you actually use. Vite, webpack, Rollup, and Parcel all handle this. For server-side use this is less of a concern.

Examples

These examples are extracted from test cases that automatically verify our functions against their native counterparts.

#codeexpected result
1ctype_alnum('AbC12')true

Dependencies

This function uses the following Locutus functions:

Here's what our current TypeScript equivalent to PHP's ctype_alnum looks like.

import { getCtypePattern } from '../_helpers/_ctypePattern.ts'
import { setlocale } from '../strings/setlocale.ts'

export function ctype_alnum(text: string): boolean {
// discuss at: https://locutus.io/php/ctype_alnum/
// parity verified: PHP 8.3
// original by: Brett Zamir (https://brett-zamir.me)
// example 1: ctype_alnum('AbC12')
// returns 1: true

if (typeof text !== 'string') {
return false
}

setlocale('LC_ALL', 0)

const pattern = getCtypePattern('an')
return pattern instanceof RegExp ? pattern.test(text) : false
}

Improve this function

Locutus is a community effort following The McDonald's Theory: we ship first iterations, hoping others will improve them. If you see something that could be better, we'd love your contribution.

View on GitHub · Edit on GitHub · View Raw


« More PHP ctype functions


Star