str_contains

(PHP 8)

str_containsDetermine if a string contains a given substring

Descripción

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

Performs a case-sensitive check indicating if needle is contained in haystack.

Parámetros

haystack

The string to search in.

needle

The substring to search for in the haystack.

Valores devueltos

Returns true if needle is in haystack, false otherwise.

Ejemplos

Ejemplo #1 Using the empty string ''

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

El resultado del ejemplo sería:

Checking the existence of the empty string will always return true

Ejemplo #2 Showing case-sensitivity

<?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';
}

?>

El resultado del ejemplo sería:

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

Notas

Nota: Esta función es segura binariamente.

Ver también

  • str_ends_with() - Checks if a string ends with a given substring
  • str_starts_with() - Checks if a string starts with a given substring
  • stripos() - Encuentra la posición de la primera aparición de un substring en un string sin considerar mayúsculas ni minúsculas
  • strrpos() - Encuentra la posición de la última aparición de un substring en un string
  • strripos() - Encuentra la posición de la última aparición de un substring insensible a mayúsculas y minúsculas en un string
  • strstr() - Encuentra la primera aparición de un string
  • strpbrk() - Buscar una cadena por cualquiera de los elementos de un conjunto de caracteres
  • substr() - Devuelve parte de una cadena
  • preg_match() - Realiza una comparación con una expresión regular
To Top