How to case-insensitively check if a character exists in a string with PHP

1 Answer

0 votes
// Case-insensitive check without a loop
function charExistsIgnoreCase(string $s, string $target): bool {
    // Convert both to lowercase and use str_contains()
    return str_contains(strtolower($s), strtolower($target));
}

// Define the string we want to search in
$s = "PHPLanguage";

// Perform the case-insensitive check
$exists = charExistsIgnoreCase($s, 'p');

// Print the raw boolean result
var_export($exists);
echo "\n";

// Conditional check
if ($exists) {
    echo "exists\n";
} else {
    echo "not exists\n";
}



/*
run:

true
exists

*/

 



answered 3 hours ago by avibootz

Related questions

...