Check if string starts with certain characters in PHP
Snippet
You can check if a certain string is the exact start of another string:
<?php
function startsWith($string, $startString) {
return mb_strpos($string, $startString) === 0;
}
// usage
echo startsWith("cat", "ca"); // true
echo startsWith("dog", "x"); // false
If you want to check case insensitively(Unlike the mb_strpos()
, mb_stripos()
is case-insensitive.):
<?php
function startsWith($string, $startString) {
return mb_stripos($string, $startString) === 0;
}
// usage
echo startsWith("cat", "ca"); // true
echo startsWith("dog", "x"); // false