Longhorn PHP 2023 - Call for Papers

str_contains

(PHP 8)

str_containsОпределяет, содержит ли строка заданную подстроку

Описание

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

Выполняет проверку с учётом регистра, указывающую, содержится ли needle в haystack.

Список параметров

haystack

Строка для поиска.

needle

Подстрока для поиска в haystack.

Возвращаемые значения

Возвращает true, если needle содержится в haystack, false в противном случае.

Примеры

Пример #1 Пример использования пустой строки ''

<?php
if (str_contains('абв', '')) {
echo
"Проверка существования пустой строки всегда возвращает true";
}
?>

Результат выполнения данного примера:

Проверка существования пустой строки всегда возвращает true

Пример #2 Демонстрация чувствительности к регистру

<?php
$string
= 'ленивая лиса перепрыгнула через забор';

if (
str_contains($string, 'ленивая')) {
echo
"Строка 'ленивая' найдена в проверяемой строке\n";
}

if (
str_contains($string, 'Ленивая')) {
echo
'Строка "Ленивая" найдена в проверяемой строке';
} else {
echo
'"Ленивая" не найдена потому что регистр не совпадает';
}

?>

Результат выполнения данного примера:

Строка 'ленивая' найдена в проверяемой строке
"Ленивая" не найдена потому что регистр не совпадает

Примечания

Замечание: Эта функция безопасна для обработки данных в двоичной форме.

Смотрите также

  • str_ends_with() - Проверяет, заканчивается ли строка заданной подстрокой
  • str_starts_with() - Проверяет, начинается ли строка с заданной подстроки
  • stripos() - Возвращает позицию первого вхождения подстроки без учёта регистра
  • strrpos() - Возвращает позицию последнего вхождения подстроки в строке
  • strripos() - Возвращает позицию последнего вхождения подстроки без учёта регистра
  • strstr() - Находит первое вхождение подстроки
  • strpbrk() - Ищет в строке любой символ из заданного набора
  • substr() - Возвращает подстроку
  • preg_match() - Выполняет проверку на соответствие регулярному выражению

add a note

User Contributed Notes 7 notes

up
89
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
6
aisunny7 dot xy at gmail dot com
1 year ago
The polyfill that based on original work from the PHP Laravel framework had a different behavior;

when the $needle is `""` or `null`:
php8's will return `true`;
but, laravel'str_contains will return `false`;

when php8.1, null is deprecated, You can use `$needle ?: ""`;
up
2
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
0
drupalista dot com dot br at gmail dot com
1 year ago
private function contains(array $needles, string $type, string $haystack = NULL, string $filename = NULL) : bool {
        if (empty($needles)) return FALSE;
        if ($filename)
            $haystack = file_get_contents($filename);

        $now_what = function(string $needle) use ($haystack, $type) : array {
            $has_needle = str_contains($haystack, $needle);
            if ($type === 'any' && $has_needle)
                return ['done' => TRUE, 'return' => TRUE];

            if ($type === 'all' && !$has_needle)
                return ['done' => TRUE, 'return' => FALSE];

            return ['done' => FALSE];
        };

        foreach ($needles as $needle) {
            $check = $now_what($needle);
            if ($check['done'])
                return $check['return'];
        }
        return TRUE;
    }

    function containsAny(array $needles, string $haystack = NULL, string $filename = NULL) : bool {
        return self::contains($needles, 'any', $haystack, $filename);
    }

    function containsAll(array $needles, string $haystack = NULL, string $filename = NULL) : bool {
        return self::contains($needles, 'all', $haystack, $filename);
    }
up
1
juliyvchirkov at gmail dot com
1 year 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
-12
AuxData
1 year ago
Until PHP 8 was released, many-a-programmer were writing our own contain() functions. Mine also handles needles with logical ORs (set to '||').
Here it is.

function contains($haystack, $needle, $offset){
    $OR = '||';
    $result = false;
   
    $ORpos = strpos($needle, $OR, 0);
    if($ORpos !== false){ //ORs exist in the needle string
        $needle_arr = explode($OR, $needle);
        for($i=0; $i < count($needle_arr); $i++){
            $pos = strpos($haystack, trim($needle_arr[$i]), $offset);
            if($pos !== false){
                $result = true;
                break;
            }
        }       
    } else {
        $pos = strpos($haystack, trim($needle), $offset);
        if($pos !== false){
          $result = true;
        }
    }
  return($result);
}

Call: contains("Apple Orange Banana", "Apple || Walnut", 0);
Returns: true
up
-48
kadenskinner at gmail dot com
2 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';
}
To Top