str_contains

(PHP 8)

str_contains指定された部分文字列が、文字列に含まれるかを調べる

説明

str_contains(string $haystack, string $needle): bool

needlehaystack に含まれるかを調べます。 大文字小文字は区別されます。

パラメータ

haystack

検索対象の文字列

needle

haystack で調べる部分文字列

戻り値

haystackneedle が含まれていた場合 true そうでない場合、false を返します。

例1 空文字列 '' を使った場合

<?php
if (str_contains('abc', '')) {
echo
"Checking the existence of the empty string will always return true";
}
?>

上の例の出力は以下となります。

Checking the existence of the empty string will always return true

例2 大文字小文字の区別

<?php
$string
= 'The lazy fox jumped over the fence';

if (
str_contains($string, 'lazy')) {
echo
"The string 'lazy' was found in the string\n";
}

if (
str_contains($string, 'Lazy')) {
echo
'The string "Lazy" was found in the string';
} else {
echo
'"Lazy" was not found because the case does not match';
}

?>

上の例の出力は以下となります。

The string 'lazy' was found in the string
"Lazy" was not found because the case does not match

注意

注意: この関数はバイナリデータに対応しています。

参考

  • str_ends_with() - 文字列が、指定された文字列で終わるかを調べる。
  • str_starts_with() - 文字列が指定された部分文字列で始まるかを調べる
  • stripos() - 大文字小文字を区別せずに文字列が最初に現れる位置を探す
  • strrpos() - 文字列中に、ある部分文字列が最後に現れる場所を探す
  • strripos() - 文字列中で、特定の(大文字小文字を区別しない)文字列が最後に現れた位置を探す
  • strstr() - 文字列が最初に現れる位置を見つける
  • strpbrk() - 文字列の中から任意の文字を探す
  • substr() - 文字列の一部分を返す
  • preg_match() - 正規表現によるマッチングを行う

add a note add a note

User Contributed Notes 5 notes

up
20
scm6079
2 years ago
For earlier versions of PHP, you can polyfill the str_contains function using the following snippet:

<?php
// based on original work from the PHP Laravel framework
if (!function_exists('str_contains')) {
    function
str_contains($haystack, $needle) {
        return
$needle !== '' && mb_strpos($haystack, $needle) !== false;
    }
}
?>
up
3
olivertasche+nospam at gmail dot com
2 years ago
The code from "me at daz dot co dot uk" will not work if the word is
- at the start of the string
- at the end of the string
- at the end of a sentence (like "the ox." or "is that an ox?")
- in quotes
- and so on.

You should explode the string by whitespace, punctations, ... and check if the resulting array contains your word OR try to test with a RegEx like this:
(^|[\s\W])+word($|[\s\W])+

Disclaimer: The RegEx may need some tweaks
up
1
juliyvchirkov at gmail dot com
2 years ago
<?php

// Polyfill for PHP 4 - PHP 7, safe to utilize with PHP 8

if (!function_exists('str_contains')) {
    function
str_contains (string $haystack, string $needle)
    {
        return empty(
$needle) || strpos($haystack, $needle) !== false;
    }
}
up
-11
kadenskinner at gmail dot com
3 years ago
<?php

$needle
= '@';
$haystack = 'user@example.com';

if (!
str_contains($haystack, $needle)){
echo
'There is not an @ in haystack';
}else{
echo
'There is an @ in haystack';
}
up
-39
me at daz dot co dot uk
3 years ago
# Watch out for aberrant partial matches

$string = 'The lazy fox jumped over the fence';

if (str_contains($string, 'ox')) {
    echo 'The string "ox" was found in the string because it was a partial match';
} else {
    echo '"ox" was not found';
}
//output: The string "ox" was found in the string because it was a partial match

# use spaces for full word matching

$string = 'The lazy fox jumped over the fence';

if (str_contains($string, ' ox ')) {
    echo 'The string " ox " was found in the string because it was a partial match';
} else {
    echo '" ox " was not found';
}
//output:  " ox " was not found
To Top