---
warning: This file is auto generated by `yarn web:inject`, do not edit by hand
examples:
- strchr('Kevin van Zonneveld', 'van')
- strchr('Kevin van Zonneveld', 'van', true)
returns:
- '''van Zonneveld'''
- '''Kevin '''
dependencies:
- php/strings/strstr
authors:
original by:
- Philip Peterson
notes: []
parityVerified:
- PHP 8.3
isTypescript: true
type: function
layout: function
title: PHP's strchr in TypeScript
description: >-
Here's what our current TypeScript equivalent to PHP's strchr looks
like.
function: strchr
category: strings
language: php
permalink: php/strings/strchr/
alias:
- /functions/php/strchr/
- /functions/strings/strchr/
- /php/strchr/
- /functions/strchr/
---
{% codeblock lang:typescript %}import { strstr } from '../strings/strstr.ts'
export function strchr(haystack: string, needle: string, bool?: boolean): string | false {
// discuss at: https://locutus.io/php/strchr/
// parity verified: PHP 8.3
// original by: Philip Peterson
// example 1: strchr('Kevin van Zonneveld', 'van')
// returns 1: 'van Zonneveld'
// example 2: strchr('Kevin van Zonneveld', 'van', true)
// returns 2: 'Kevin '
return strstr(haystack, needle, bool)
}
{% endcodeblock %}
{% codeblock lang:javascript %}import { strstr } from '../strings/strstr.ts'
export function strchr(haystack, needle, bool) {
// discuss at: https://locutus.io/php/strchr/
// parity verified: PHP 8.3
// original by: Philip Peterson
// example 1: strchr('Kevin van Zonneveld', 'van')
// returns 1: 'van Zonneveld'
// example 2: strchr('Kevin van Zonneveld', 'van', true)
// returns 2: 'Kevin '
return strstr(haystack, needle, bool)
}{% endcodeblock %}
{% codeblock lang:typescript %}// php/strings/strstr (Locutus dependency module)
function strstr(haystack: string, needle: string, bool?: boolean): string | false {
// discuss at: https://locutus.io/php/strstr/
// parity verified: PHP 8.3
// original by: Kevin van Zonneveld (https://kvz.io)
// bugfixed by: Onno Marsman (https://twitter.com/onnomarsman)
// improved by: Kevin van Zonneveld (https://kvz.io)
// example 1: strstr('Kevin van Zonneveld', 'van')
// returns 1: 'van Zonneveld'
// example 2: strstr('Kevin van Zonneveld', 'van', true)
// returns 2: 'Kevin '
// example 3: strstr('name@example.com', '@')
// returns 3: '@example.com'
// example 4: strstr('name@example.com', '@', true)
// returns 4: 'name'
let pos = 0
haystack += ''
pos = haystack.indexOf(needle)
if (pos === -1) {
return false
} else {
if (bool) {
return haystack.substr(0, pos)
} else {
return haystack.slice(pos)
}
}
}
// php/strings/strchr (target function module)
function strchr(haystack: string, needle: string, bool?: boolean): string | false {
// discuss at: https://locutus.io/php/strchr/
// parity verified: PHP 8.3
// original by: Philip Peterson
// example 1: strchr('Kevin van Zonneveld', 'van')
// returns 1: 'van Zonneveld'
// example 2: strchr('Kevin van Zonneveld', 'van', true)
// returns 2: 'Kevin '
return strstr(haystack, needle, bool)
}{% endcodeblock %}
{% codeblock lang:javascript %}// php/strings/strstr (Locutus dependency module)
function strstr(haystack, needle, bool) {
// discuss at: https://locutus.io/php/strstr/
// parity verified: PHP 8.3
// original by: Kevin van Zonneveld (https://kvz.io)
// bugfixed by: Onno Marsman (https://twitter.com/onnomarsman)
// improved by: Kevin van Zonneveld (https://kvz.io)
// example 1: strstr('Kevin van Zonneveld', 'van')
// returns 1: 'van Zonneveld'
// example 2: strstr('Kevin van Zonneveld', 'van', true)
// returns 2: 'Kevin '
// example 3: strstr('name@example.com', '@')
// returns 3: '@example.com'
// example 4: strstr('name@example.com', '@', true)
// returns 4: 'name'
let pos = 0
haystack += ''
pos = haystack.indexOf(needle)
if (pos === -1) {
return false
} else {
if (bool) {
return haystack.substr(0, pos)
} else {
return haystack.slice(pos)
}
}
}
// php/strings/strchr (target function module)
function strchr(haystack, needle, bool) {
// discuss at: https://locutus.io/php/strchr/
// parity verified: PHP 8.3
// original by: Philip Peterson
// example 1: strchr('Kevin van Zonneveld', 'van')
// returns 1: 'van Zonneveld'
// example 2: strchr('Kevin van Zonneveld', 'van', true)
// returns 2: 'Kevin '
return strstr(haystack, needle, bool)
}{% endcodeblock %}