/src/php/strings/stristr.js
JavaScript | 25 lines | 16 code | 2 blank | 7 comment | 4 complexity | 3d46bbeca781f9bd4af7e81c98428e82 MD5 | raw file
- module.exports = function stristr (haystack, needle, bool) {
- // discuss at: https://locutus.io/php/stristr/
- // original by: Kevin van Zonneveld (https://kvz.io)
- // bugfixed by: Onno Marsman (https://twitter.com/onnomarsman)
- // example 1: stristr('Kevin van Zonneveld', 'Van')
- // returns 1: 'van Zonneveld'
- // example 2: stristr('Kevin van Zonneveld', 'VAN', true)
- // returns 2: 'Kevin '
- let pos = 0
- haystack += ''
- pos = haystack.toLowerCase()
- .indexOf((needle + '')
- .toLowerCase())
- if (pos === -1) {
- return false
- } else {
- if (bool) {
- return haystack.substr(0, pos)
- } else {
- return haystack.slice(pos)
- }
- }
- }