I need 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 true
echo endsWith($str,'}'); //Returns true
I need 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 true
echo endsWith($str,'}'); //Returns true
Here you go:
function startsWith($haystack,$needle,$case=true) {
if($case){return (strcmp(substr($haystack, 0, strlen($needle)),$needle)===0);}
return (strcasecmp(substr($haystack, 0, strlen($needle)),$needle)===0);
}
function endsWith($haystack,$needle,$case=true) {
if($case){return (strcmp(substr($haystack, strlen($haystack) - strlen($needle)),$needle)===0);}
return (strcasecmp(substr($haystack, strlen($haystack) - strlen($needle)),$needle)===0);
}
Credit to:
Your best bet would be to use regular expressions and your functions would look something like this...
function startsWith($needle, $haystack)
{
return preg_match('^'.$needle, $haystack);
}
function endsWith($needle, $haystack)
{
return preg_match($needle .'$', $haystack);
}
function startsWith($string, $char)
{
$length = strlen($char);
return (substr($string, 0, $length) === $char);
}
function endsWith($string, $char)
{
$length = strlen($char);
$start = $length *-1; //negative
return (substr($string, $start, $length) === $char);
}
Use this if you don't want to use a regex.
All answers above 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 in $haystack
function startsWith($haystack,$needle,$case=true)
{
if($case)
return strpos($haystack, $needle, 0) === 0;
return stripos($haystack, $needle, 0) === 0;
}
function endsWith($haystack,$needle,$case=true)
{
$expectedPosition = strlen($haystack) - strlen($needle);
if($case)
return strrpos($haystack, $needle, 0) === $expectedPosition;
return strripos($haystack, $needle, 0) === $expectedPosition;
}
The regex functions above, but with the other tweaks also suggested above:
function startsWith($needle, $haystack)
{
return preg_match('/^'.preg_quote($needle)."/", $haystack);
}
function endsWith($needle, $haystack)
{
return preg_match("/".preg_quote($needle) .'$/', $haystack);
}
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 ($case)
return strncasecmp($haystack, $needle, strlen($needle)) == 0;
else
return strncmp($haystack, $needle, strlen($needle)) == 0;
}
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 strncasecmp($haystack, $needle, strlen($needle)) == 0;
}
function endsWith($haystack, $needle, $case=true) {
return startsWith(strrev($haystack),strrev($needle),$case);
}
Note: I have swapped the if-else part for James Black's startsWith function, because strncasecmp is actually the case-insensitive version of strncmp.
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 character
function startsWithChar($needle, $haystack)
{
return ($needle[0] === $haystack);
}
function endsWithChar($needle, $haystack)
{
return ($needle[strlen($needle) - 1] === $haystack);
}
$str='|apples}';
echo startsWithChar($str,'|'); //Returns true
echo endsWithChar($str,'}'); //Returns true
echo startsWithChar($str,'='); //Returns false
echo endsWithChar($str,'#'); //Returns false
Here's just a fix on Sander Rijken's method. The $expectedPosition
calculation is slightly off in endsWith
. (The last -1
is not necessary).
function startsWith($string, $prefix, $caseSensitive = true) {
if(!$caseSensitive) {
return stripos($string, $prefix, 0) === 0;
}
return strpos($string, $prefix, 0) === 0;
}
function endsWith($string, $postfix, $caseSensitive = true) {
$expectedPostition = strlen($string) - strlen($postfix);
if(!$caseSensitive) {
return strripos($string, $postfix, 0) === $expectedPostition;
}
return strrpos($string, $postfix, 0) === $expectedPostition;
}
My personal preference would be eliminating all code repeats, so here is another version of Sander Rijken's method:
function contains($haystack, $needle, $case = true, $pos = 0)
{
if ($case)
{
$result = (strpos($haystack, $needle, 0) === $pos);
}
else
{
$result = (stripos($haystack, $needle, 0) === $pos);
}
return $result;
}
function starts_with($haystack, $needle, $case = true)
{
return contains($haystack, $needle, $case, 0);
}
function ends_with($haystack, $needle, $case = true)
{
return contains($haystack, $needle, $case, (strlen($haystack) - strlen($needle)));
}