W3Schools
w3schools.com › php › func_string_str_starts_with.asp
PHP str_starts_with() Function
Check if a string starts with a specific substring.
PHP Tutorial
phptutorial.net › home › php tutorial › php str_starts_with
PHP str_starts_with(): Check If a String starts with a Substring
May 8, 2021 - <?php $str = 'PHP tutorial'; $substr = 'PHP'; $result = str_starts_with($str, $substr) ? 'is' : 'is not'; echo "The $str $result starting with $substr";Copy ... It’s important to keep in mind that the str_starts_with() function performs a case-sensitive search.
Codecademy
codecademy.com › docs › php › string functions › str_starts_with()
PHP | String Functions | str_starts_with() | Codecademy
June 28, 2023 - Performs a case-sensitive search to check if a string starts with a given substring.
W3Resource
w3resource.com › php-exercises › php-basic-exercise-95.php
PHP Exercise: Check if a given string starts with a given substring - w3resource
March 31, 2026 - <?php // Licence: https://bit.ly/2CFA5XY // Function definition for 'startsWith' that takes a haystack string and a needle string as parameters function startsWith($haystack, $needle) { // Use 'strpos' to find the position of the needle in the haystack // Check if the position is at the beginning (index 0) of the haystack return strpos($haystack, $needle) === 0; } // Call 'startsWith' with a haystack string and a needle string, then display the result using 'print_r' print_r(startsWith('Hi, this is me', 'Hi')); ?>
W3Schools
w3schools.com › python › ref_string_startswith.asp
Python String startswith() Method
Well organized and easy to understand Web building tutorials with lots of examples of how to use HTML, CSS, JavaScript, SQL, Python, PHP, Bootstrap, Java, XML and more.
Jonasjohn
jonasjohn.de › snippets › php › starts-with.htm
PHP Starts with - Jonas John
March 28, 2006 - * * @param string * @param string * @return bool */ function StartsWith($Haystack, $Needle){ // Recommended version, using strpos return strpos($Haystack, $Needle) === 0; } // Another way, using substr function StartsWithOld($Haystack, $Needle){ return substr($Haystack, 0, strlen($Needle)) == $Needle; }
Tutorialspoint
tutorialspoint.com › php › php_str_starts_with_function.htm
PHP String str_starts_with() Function
Below is the syntax of the PHP String str_starts_with() function − · bool str_starts_with ( string $string, string $search ); Here are the parameters of the str_starts_with() function − · $string − (Required) It is the string to search in.
OnlinePHP
onlinephp.io › str-starts-with
str_starts_with - Online Tool
Execute and test str_starts_with with this online tool
Codemia
codemia.io › home › knowledge hub › startswith() and endswith() functions in php
startsWith() and endsWith() functions in PHP
December 28, 2024 - PHP 8.0 introduced built-in str_starts_with() and str_ends_with() functions that check whether a string begins or ends with a given substring. Before PHP...
W3Schools
w3schools.com › php › php_ref_string.asp
PHP String Functions
The PHP string functions are part of the PHP core. No installation is required to use these functions. ... Ad-free learning, track your progress, earn XP, streaks, compete in leagues, build and host websites, unlock coding challenges, and much more! ... If you want to use W3Schools services as an educational institution, team or enterprise, send us an e-mail: sales@w3schools.com
Université Pierre et Marie Curie
www-ppti.ufr-info-p6.jussieu.fr › doc-online › PHP › php-chunked-xhtml › function.str-starts-with.html
Checks if a string starts with a given substring
<?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'; } ?>
Top answer 1 of 16
1872
PHP 8.0 and higher
Since PHP 8.0 you can use:
str_starts_with and
str_ends_with
Example
var_dump(str_starts_with('|apples}', '|'));
var_dump(str_ends_with('|apples}', '}'));
PHP before 8.0
function startsWith( $haystack, $needle ) {
$length = strlen( $needle );
return substr( $haystack, 0, $length ) === $needle;
}
function endsWith( $haystack, $needle ) {
$length = strlen( $needle );
if( !$length ) {
return true;
}
return substr( $haystack, -$length ) === $needle;
}
2 of 16
1105
PHP < 8
You can use substr_compare function to check start-with and ends-with:
function startsWith($haystack, $needle) {
return substr_compare($haystack, $needle, 0, strlen($needle)) === 0;
}
function endsWith($haystack, $needle) {
return substr_compare($haystack, $needle, -strlen($needle)) === 0;
}
This should be one of the fastest solutions on PHP 7 (benchmark script). Tested against 8KB haystacks, various length needles and full, partial and no match cases. strncmp is a touch faster for starts-with but it cannot check ends-with.