str_starts_with and str_ends_with functions in PHP
How can I write two functions that would take a string and return if it starts with the specified character/string or ends with it?For example:$str = '|apples}';echo startsWith($str, '|'); //Returns...
View ArticleAnswer by KdgDev for str_starts_with and str_ends_with functions in PHP
function startsWith($haystack, $needle, $case = true) { if ($case) { return (strcmp(substr($haystack, 0, strlen($needle)), $needle) === 0); } return (strcasecmp(substr($haystack, 0, strlen($needle)),...
View ArticleAnswer by MrHus for str_starts_with and str_ends_with functions in PHP
PHP 8.0 and higherSince PHP 8.0 you can use:str_starts_with andstr_ends_withExamplevar_dump(str_starts_with('|apples}', '|'));var_dump(str_ends_with('|apples}', '}'));PHP before 8.0function startsWith(...
View ArticleAnswer by Sander Rijken for str_starts_with and str_ends_with functions in PHP
All answers so far seem to do loads of unnecessary work, strlen calculations, string allocations (substr), etc. The 'strpos' and 'stripos' functions return the index of the first occurrence of $needle...
View ArticleAnswer by tridian for str_starts_with and str_ends_with functions in PHP
The regex functions above, but with the other tweaks also suggested above: function startsWith($needle, $haystack) { return preg_match('/^' . preg_quote($needle, '/') . '/', $haystack); } function...
View ArticleAnswer by James Black for str_starts_with and str_ends_with functions in PHP
I realize this has been finished, but you may want to look at strncmp as it allows you to put the length of the string to compare against, so:function startsWith($haystack, $needle, $case=true) { if...
View ArticleAnswer by bobo for str_starts_with and str_ends_with functions in PHP
Based on James Black's answer, here is its endsWith version:function startsWith($haystack, $needle, $case=true) { if ($case) return strncmp($haystack, $needle, strlen($needle)) == 0; else return...
View ArticleAnswer by lepe for str_starts_with and str_ends_with functions in PHP
If speed is important for you, try this.(I believe it is the fastest method)Works only for strings and if $haystack is only 1 characterfunction startsWithChar($needle, $haystack){ return ($needle ===...
View ArticleAnswer by Freeman for str_starts_with and str_ends_with functions in PHP
You also can use regular expressions:function endsWith($haystack, $needle, $case=true) { return preg_match("/.*{$needle}$/" . (($case) ? "" : "i"), $haystack);}
View ArticleAnswer by Patrick Smith for str_starts_with and str_ends_with functions in PHP
Here’s an efficient solution for PHP 4. You could get faster results if on PHP 5 by using substr_compare instead of strcasecmp(substr(...)).function stringBeginsWith($haystack, $beginning,...
View ArticleAnswer by Dan for str_starts_with and str_ends_with functions in PHP
Short and easy-to-understand one-liners without regular expressions.startsWith() is straight forward.function startsWith($haystack, $needle) { return (strpos($haystack, $needle) === 0);}endsWith() uses...
View ArticleAnswer by mpen for str_starts_with and str_ends_with functions in PHP
Updated 23-Aug-2016Functionsfunction substr_startswith($haystack, $needle) { return substr($haystack, 0, strlen($needle)) === $needle;}function preg_match_startswith($haystack, $needle) { return...
View ArticleAnswer by biziclop for str_starts_with and str_ends_with functions in PHP
The substr function can return false in many special cases, so here is my version, which deals with these issues:function startsWith( $haystack, $needle ){ return $needle === ''.substr( $haystack, 0,...
View ArticleAnswer by Vincent Pazeller for str_starts_with and str_ends_with functions in...
in short:function startsWith($str, $needle){ return substr($str, 0, strlen($needle)) === $needle;}function endsWith($str, $needle){ $length = strlen($needle); return !$length || substr($str, - $length)...
View ArticleAnswer by Salman Arshad for str_starts_with and str_ends_with functions in PHP
PHP < 8You 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...
View ArticleAnswer by Bill Effin Murray for str_starts_with and str_ends_with functions...
Why not the following?//How to check if a string begins with another string$haystack = "valuehaystack";$needle = "value";if (strpos($haystack, $needle) === 0){ echo "Found " . $needle . " at the...
View ArticleAnswer by user507410 for str_starts_with and str_ends_with functions in PHP
This may workfunction startsWith($haystack, $needle) { return substr($haystack, 0, strlen($needle)) == $needle;}Source: https://stackoverflow.com/a/4419658
View ArticleAnswer by FrancescoMM for str_starts_with and str_ends_with functions in PHP
Focusing on startswith, if you are sure strings are not empty, adding a test on the first char, before the comparison, the strlen, etc., speeds things up a bit:function startswith5b($haystack, $needle)...
View ArticleAnswer by Ja͢ck for str_starts_with and str_ends_with functions in PHP
Here are two functions that don't introduce a temporary string, which could be useful when needles are substantially big:function startsWith($haystack, $needle){ return strncmp($haystack, $needle,...
View ArticleAnswer by Srinivasan.S for str_starts_with and str_ends_with functions in PHP
I hope that the below answer may be efficient and also simple:$content = "The main string to search";$search = "T";//For compare the begining string with case insensitive. if(stripos($content, $search)...
View Article