Install via yarn add locutus and import:
import { trim } from 'locutus/php/strings/trim'.
Or with CommonJS: const { trim } = require('locutus/php/strings/trim')
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
trim(' Kevin van Zonneveld ')
'Kevin van Zonneveld'
2
trim('Hello World', 'Hdle')
'o Wor'
3
trim(16, 1)
'6'
Here's what our current TypeScript equivalent to PHP's trim looks like.
exportfunctiontrim(str: string | number, charlist?: string | number): string { // discuss at: https://locutus.io/php/trim/ // parity verified: PHP 8.3 // original by: Kevin van Zonneveld (https://kvz.io) // improved by: mdsjack (https://www.mdsjack.bo.it) // improved by: Alexander Ermolaev (https://snippets.dzone.com/user/AlexanderErmolaev) // improved by: Kevin van Zonneveld (https://kvz.io) // improved by: Steven Levithan (https://blog.stevenlevithan.com) // improved by: Jack // input by: Erkekjetter // input by: DxGx // bugfixed by: Onno Marsman (https://twitter.com/onnomarsman) // example 1: trim(' Kevin van Zonneveld ') // returns 1: 'Kevin van Zonneveld' // example 2: trim('Hello World', 'Hdle') // returns 2: 'o Wor' // example 3: trim(16, 1) // returns 3: '6'
let whitespace = [ ' ', '\n', '\r', '\t', '\f', '\x0b', '\xa0', '\u2000', '\u2001', '\u2002', '\u2003', '\u2004', '\u2005', '\u2006', '\u2007', '\u2008', '\u2009', '\u200a', '\u200b', '\u2028', '\u2029', '\u3000', ].join('') let l = 0 let i = 0 let strValue = String(str)
if (charlist) { whitespace = (charlist + '').replace(/([[\]().?/*{}+$^:])/g, '$1') }
l = strValue.length for (i = 0; i < l; i++) { if (!whitespace.includes(strValue.charAt(i))) { strValue = strValue.substring(i) break } }
l = strValue.length for (i = l - 1; i >= 0; i--) { if (!whitespace.includes(strValue.charAt(i))) { strValue = strValue.substring(0, i + 1) break } }
exportfunctiontrim(str, charlist) { // discuss at: https://locutus.io/php/trim/ // parity verified: PHP 8.3 // original by: Kevin van Zonneveld (https://kvz.io) // improved by: mdsjack (https://www.mdsjack.bo.it) // improved by: Alexander Ermolaev (https://snippets.dzone.com/user/AlexanderErmolaev) // improved by: Kevin van Zonneveld (https://kvz.io) // improved by: Steven Levithan (https://blog.stevenlevithan.com) // improved by: Jack // input by: Erkekjetter // input by: DxGx // bugfixed by: Onno Marsman (https://twitter.com/onnomarsman) // example 1: trim(' Kevin van Zonneveld ') // returns 1: 'Kevin van Zonneveld' // example 2: trim('Hello World', 'Hdle') // returns 2: 'o Wor' // example 3: trim(16, 1) // returns 3: '6'
let whitespace = [ ' ', '\n', '\r', '\t', '\f', '\x0b', '\xa0', '\u2000', '\u2001', '\u2002', '\u2003', '\u2004', '\u2005', '\u2006', '\u2007', '\u2008', '\u2009', '\u200a', '\u200b', '\u2028', '\u2029', '\u3000', ].join('') let l = 0 let i = 0 let strValue = String(str)
if (charlist) { whitespace = (charlist + '').replace(/([[\]().?/*{}+$^:])/g, '$1') } l = strValue.length for (i = 0; i < l; i++) { if (!whitespace.includes(strValue.charAt(i))) { strValue = strValue.substring(i) break } } l = strValue.length for (i = l - 1; i >= 0; i--) { if (!whitespace.includes(strValue.charAt(i))) { strValue = strValue.substring(0, i + 1) break } } return whitespace.includes(strValue.charAt(0)) ? '' : strValue }
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.