Install via yarn add locutus and import:
import { strtok } from 'locutus/php/strings/strtok'.
Or with CommonJS: const { strtok } = require('locutus/php/strings/strtok')
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.
#
code
expected result
1
var $string = "\t\t\t\nThis is\tan example\nstring\n"
var $tok = strtok($string, " \n\t")
var $b = ''
while ($tok !== false) {$b += "Word="+$tok+"\n"; $tok = strtok(" \n\t");}
var $result = $b
exportfunctionstrtok(str: string, tokens?: string): string | false { // discuss at: https://locutus.io/php/strtok/ // original by: Brett Zamir (https://brett-zamir.me) // note 1: Use tab and newline as tokenizing characters as well // example 1: var $string = "\t\t\t\nThis is\tan example\nstring\n" // example 1: var $tok = strtok($string, " \n\t") // example 1: var $b = '' // example 1: while ($tok !== false) {$b += "Word="+$tok+"\n"; $tok = strtok(" \n\t");} // example 1: var $result = $b // returns 1: "Word=This\nWord=is\nWord=an\nWord=example\nWord=string\n"
if (typeof tokens === 'undefined') { tokens = str str = getPhpRuntimeString('strtokleftOver', '') } if (str.length === 0) { returnfalse } if (tokens.includes(str.charAt(0))) { returnstrtok(str.substring(1), tokens) } let i = 0 for (; i < str.length; i++) { if (tokens.includes(str.charAt(i))) { break } } setPhpRuntimeEntry('strtokleftOver', str.substring(i + 1))
exportfunctionstrtok(str, tokens) { // discuss at: https://locutus.io/php/strtok/ // original by: Brett Zamir (https://brett-zamir.me) // note 1: Use tab and newline as tokenizing characters as well // example 1: var $string = "\t\t\t\nThis is\tan example\nstring\n" // example 1: var $tok = strtok($string, " \n\t") // example 1: var $b = '' // example 1: while ($tok !== false) {$b += "Word="+$tok+"\n"; $tok = strtok(" \n\t");} // example 1: var $result = $b // returns 1: "Word=This\nWord=is\nWord=an\nWord=example\nWord=string\n"
if (typeof tokens === 'undefined') { tokens = str str = getPhpRuntimeString('strtokleftOver', '') } if (str.length === 0) { returnfalse } if (tokens.includes(str.charAt(0))) { returnstrtok(str.substring(1), tokens) } let i = 0 for (; i < str.length; i++) { if (tokens.includes(str.charAt(i))) { break } } setPhpRuntimeEntry('strtokleftOver', str.substring(i + 1))
typePhpRuntimeStringKey = { [K in keyof PhpRuntimeKnownEntryMap]: PhpRuntimeKnownEntryMap[K] extendsstring ? K : never }[keyof PhpRuntimeKnownEntryMap]
functiongetPhpRuntimeString(key: string, fallback: string): string { const value = getPhpRuntimeEntry(key) returntypeof value === 'string' ? value : fallback }
// php/strings/strtok (target function module) functionstrtok(str: string, tokens?: string): string | false { // discuss at: https://locutus.io/php/strtok/ // original by: Brett Zamir (https://brett-zamir.me) // note 1: Use tab and newline as tokenizing characters as well // example 1: var $string = "\t\t\t\nThis is\tan example\nstring\n" // example 1: var $tok = strtok($string, " \n\t") // example 1: var $b = '' // example 1: while ($tok !== false) {$b += "Word="+$tok+"\n"; $tok = strtok(" \n\t");} // example 1: var $result = $b // returns 1: "Word=This\nWord=is\nWord=an\nWord=example\nWord=string\n"
if (typeof tokens === 'undefined') { tokens = str str = getPhpRuntimeString('strtokleftOver', '') } if (str.length === 0) { returnfalse } if (tokens.includes(str.charAt(0))) { returnstrtok(str.substring(1), tokens) } let i = 0 for (; i < str.length; i++) { if (tokens.includes(str.charAt(i))) { break } } setPhpRuntimeEntry('strtokleftOver', str.substring(i + 1))
functiongetPhpRuntimeString(key, fallback) { const value = getPhpRuntimeEntry(key) returntypeof value === 'string' ? value : fallback }
// php/strings/strtok (target function module) functionstrtok(str, tokens) { // discuss at: https://locutus.io/php/strtok/ // original by: Brett Zamir (https://brett-zamir.me) // note 1: Use tab and newline as tokenizing characters as well // example 1: var $string = "\t\t\t\nThis is\tan example\nstring\n" // example 1: var $tok = strtok($string, " \n\t") // example 1: var $b = '' // example 1: while ($tok !== false) {$b += "Word="+$tok+"\n"; $tok = strtok(" \n\t");} // example 1: var $result = $b // returns 1: "Word=This\nWord=is\nWord=an\nWord=example\nWord=string\n"
if (typeof tokens === 'undefined') { tokens = str str = getPhpRuntimeString('strtokleftOver', '') } if (str.length === 0) { returnfalse } if (tokens.includes(str.charAt(0))) { returnstrtok(str.substring(1), tokens) } let i = 0 for (; i < str.length; i++) { if (tokens.includes(str.charAt(i))) { break } } setPhpRuntimeEntry('strtokleftOver', str.substring(i + 1))
return str.substring(0, i) }
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.