Statement on glibc/iconv Vulnerability

str_starts_with

(PHP 8)

str_starts_withDétermine si une chaîne commence par une sous-chaîne donnée

Description

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

Effectue une vérification sensible à la casse indiquant si haystack commence 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 commence par needle, sinon false.

Exemples

Exemple #1 Avec une chaîne vide ''

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

L'exemple ci-dessus va afficher :

All strings start with the empty string

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

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

if (
str_starts_with($string, 'The')) {
echo
"The string starts with 'The'\n";
}

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

?>

L'exemple ci-dessus va afficher :

The string starts with 'The'
"the" 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_ends_with() - Détermine si une chaîne se termine 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 1 note

up
1
sunyanzi at aliyun dot com
2 hours ago
This function is astonishingly fast, I think you should always use it.

See my test code below, uncomment specific lines and run it yourself to see the effect.

<?php
$str
= sha1( '0' ); // b6589fc6ab0dc82cf12099d1c2d40ab994e8410c

$results = [];

for (
$j = 0; $j < 100; ++ $j ) {

$time = microtime( true );

for (
$i = 0; $i < 999999; ++ $i )

// if ( 'b6589f' === substr( $str, 0, 6 ) ); # 0.53781658887863
// if ( 'abcdef' === substr( $str, 0, 6 ) ); # 0.54560388565063

// if ( 0 === strpos( $str, 'b6589f' ) ); # 0.48515980958939
// if ( 0 === strpos( $str, 'abcdef' ) ); # 0.53281677722931

// if ( str_starts_with( $str, 'b6589f' ) ); # 0.29601020097733
// if ( str_starts_with( $str, 'abcdef' ) ); # 0.30969516038895

// if (
// 'b' === $str[0] && '6' === $str[1] && '5' === $str[2] &&
// '8' === $str[3] && '9' === $str[4] && 'f' === $str[5]
// ); # 1.6171971583366

$results[] = microtime( true ) - $time;

}

echo
array_sum( $results ) / 100;
To Top