PHP str_ends_with

Resumo : neste tutorial, você aprenderá como usar a str_ends_with()função PHP para verificar se uma string termina com uma substring.

Introdução à função PHP str_ends_with()

A str_ends_with()função realiza uma pesquisa que diferencia maiúsculas de minúsculas e verifica se uma string termina com uma substring.

Aqui está a sintaxe da str_ends_with()função:

str_ends_with ( string $haystack , string $needle ) : boolLinguagem de código:  PHP  ( php )

A str_ends_with()função possui dois parâmetros:

  • $haystacké a string de entrada a ser verificada.
  • $needleé a substring a ser pesquisada na string de entrada.

A str_ends_with()função retorna truese $haystackterminar com $needleou falsecaso contrário.

Observe que toda string termina com uma string vazia. Portanto, se $needlefor um vazio ( ''), str_ends_with()sempre retornará true.

A str_ends_with()função só está disponível desde o PHP 8.0.0. Se você usa PHP com uma versão inferior, você pode preencher a função assim:

<?php

if (!function_exists('str_ends_with')) {
    function str_ends_with($haystack, $needle)
    {
        return $needle !== '' ? substr($haystack, -strlen($needle)) === $needle : true;
    }
}Linguagem de código:  PHP  ( php )

Exemplos de funções PHP str_ends_with()

Vejamos alguns exemplos de uso da str_ends_with()função PHP.

1) Usando a função PHP str_ends_with() com um exemplo de substring vazia

O exemplo a seguir retorna trueporque toda string termina com uma string vazia:

<?php

echo str_ends_with('Hello there', '');Linguagem de código:  PHP  ( php )

Saída:

1Linguagem de código:  PHP  ( php )

2) Usando a função PHP str_ends_with() com exemplo de caractere único

O exemplo a seguir usa a str_ends_with()função PHP para verificar se a string 'PHP tutorial'termina com a letra 'l':

<?php

$str = 'PHP tutorial';
$substr = 'l';

$result = str_ends_with($str, $substr) ? 'is' : 'is not';

echo "The $str $result ending with $substr";Linguagem de código:  PHP  ( php )

Saída:

The PHP tutorial is ending with lLinguagem de código:  PHP  ( php )

3) Usando a função PHP str_ends_with() com exemplo de múltiplos caracteres

O exemplo a seguir usa a str_ends_with()função PHP para verificar se a string 'PHP tutorial'termina com string 'tutorial':

<?php 

$str = 'PHP tutorial';
$substr = 'tutorial';

$result = str_ends_with($str, $substr) ? 'is' : 'is not';

echo "The $str $result ending with $substr";Linguagem de código:  PHP  ( php )

Saída:

The PHP tutorial is ending with tutorialLinguagem de código:  PHP  ( php )

4) Exemplo com distinção entre maiúsculas e minúsculas

É importante notar que a str_ends_with()função realiza uma pesquisa que diferencia maiúsculas de minúsculas. Por exemplo:

<?php

$str = 'PHP tutorial';
$substr = 'Tutorial';

$result = str_ends_with($str, $substr) ? 'is' : 'is not';

echo "The $str $result starting with $substr";Linguagem de código:  PHP  ( php )

Saída:

The PHP tutorial is not ending with TutorialLinguagem de código:  PHP  ( php )

Resumo

  • Use a função PHP str_ends_with()para realizar uma pesquisa com distinção entre maiúsculas e minúsculas e verificar se uma string termina com uma substring.

Deixe um comentário

O seu endereço de email não será publicado. Campos obrigatórios marcados com *