Statement on glibc/iconv Vulnerability

str_ends_with

(PHP 8)

str_ends_withDétermine si une chaîne se termine par une sous-chaîne donnée

Description

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

Effectue une vérification sensible à la casse indiquant si haystack se termine par needle.

Liste de paramètres

haystack

La chaîne dans laquelle on effectue la recherche.

needle

La sous-chaîne à rechercher dans haystack.

Valeurs de retour

Renvoie true si haystack se termine par needle, sinon false.

Exemples

Exemple #1 Avec une chaîne vide ''

<?php
if (str_ends_with('abc', '')) {
echo
"All strings end with the empty string";
}
?>

L'exemple ci-dessus va afficher :

All strings end with the empty string

Exemple #2 Démonstration de la sensibilité à la casse

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

if (
str_ends_with($string, 'fence')) {
echo
"The string ends with 'fence'\n";
}

if (
str_ends_with($string, 'Fence')) {
echo
'The string ends with "Fence"';
} else {
echo
'"Fence" was not found because the case does not match';
}

?>

L'exemple ci-dessus va afficher :

The string ends with 'fence'
"Fence" was not found because the case does not match

Notes

Note: Cette fonction gère les chaînes binaires.

Voir aussi

  • str_contains() - Détermine si une chaîne contient une sous-chaîne donnée
  • str_starts_with() - Détermine si une chaîne commence par une sous-chaîne donnée
  • stripos() - Recherche la position de la première occurrence dans une chaîne, sans tenir compte de la casse
  • strrpos() - Cherche la position de la dernière occurrence d'une sous-chaîne dans une chaîne
  • strripos() - Cherche la position de la dernière occurrence d'une chaîne contenue dans une autre, de façon insensible à la casse
  • strstr() - Trouve la première occurrence dans une chaîne
  • strpbrk() - Recherche un ensemble de caractères dans une chaîne de caractères
  • substr() - Retourne un segment de chaîne
  • preg_match() - Effectue une recherche de correspondance avec une expression rationnelle standard

add a note

User Contributed Notes 3 notes

up
6
Reinder
10 months ago
In PHP7 you may want to use:

if (!function_exists('str_ends_with')) {
function str_ends_with($str, $end) {
return (@substr_compare($str, $end, -strlen($end))==0);
}
}

AFAIK that is binary safe and doesn't need additional checks.
up
6
javalc6 at gmail dot com
10 months ago
In case you are using an older version of PHP, you can define and use the following function:

function endsWith($haystack, $needle) {
$length = strlen($needle);
return $length > 0 ? substr($haystack, -$length) === $needle : true;
}
up
6
divinity76 at gmail dot com
2 years ago
this is the fastest php7-implementation i can think of, it should be faster than javalc6 and Reinder's implementations, as this one doesn't create new strings (but theirs does)

<?php
if (! function_exists('str_ends_with')) {
function
str_ends_with(string $haystack, string $needle): bool
{
$needle_len = strlen($needle);
return (
$needle_len === 0 || 0 === substr_compare($haystack, $needle, - $needle_len));
}
}
?>
To Top