Web4College

PHP string starts or ends with character or string

We can check whether a string starts or ends with a specified character or a string. We should have a function in PHP that can check the whether the string starts or ends with our specified string or character.

PHP string starts with a specified character or string

Example


function startsWithStr($string, $characterString)
{
  $length = strlen($characterString);
  return (substr($string, 0, $length)===$characterString)? true : false;
}

echo startsWithStr("web4college","we"); // 1 

This function checks whether the original string starts with our specified string or not. If there is a match it returns true otherwise returns false.


$string

We check our specified string or character at the start of this string.

$characterString

It is the specific string or character that we are going to match with the start of other string.


Let's see a few examples to understand how it works. Remember that this function is case-insensitive.

Example 1:

See the following example. We are going to match the college string with the web4college.

Example

function startsWithStr($string, $characterString)
{
  $length = strlen($characterString);
  return (substr($string, 0, $length)===$characterString)? true : false;
}

if(startsWithStr("web4college","college"))
{
  echo "Yes";
}
else
{
  echo "No";
}
// No

In the above-given example, the web4college string does not start with college. That is why it is returning false.

Example 2:

Now we check for another string.

Example


function startsWithStr($string, $characterString)
{
  $length = strlen($characterString);
  return (substr($string, 0, $length)===$characterString)? true : false;
}

if(startsWithStr("asdfe","as"))
{
  echo "Yes";
}
else
{
  echo "No";
}
// Yes

It is returning true because the asdfe is starting with as.


PHP string ends with a specified character or string

Example

function endsWithStr($string, $characterString)
{
  $length = strlen($characterString);
  return (substr($string, -$length)===$characterString)? true : false;
}

echo endsWithStr("web4college","college");  // 1 

This function checks whether the original string ends with our specified string or not. If there is a match it returns true otherwise returns false.

Let's see a few examples to understand how this function works.

Example 1:

See the following example. We are going to match the college string with the web4college.

Example

function endsWithStr($string, $characterString)
{
  $length = strlen($characterString);
  return (substr($string, -$length)===$characterString)? true : false;
}

if(endsWithStr("web4college","college"))
{
	echo "Yes";
}
else
{
	echo "No";
}
// Yes

In the above-given example, the web4college string ends with college. That is why it is returning true.

Example 2:

Now we check for another string.

Example

function endsWithStr($string, $characterString)
{
  $length = strlen($characterString);
  return (substr($string, -$length)===$characterString)? true : false;
}

if(endsWithStr("asdfe","as"))
{
  echo "Yes";
}
else
{
  echo "No";
}
// No

It is returning false because the asdfe is not ending with as.

Summary

We have made two functions, startsWithStr(), endsWithStr(). These two functions accept two arguments to check whether the string starts or ends with a specified string or not.


PHP

How to split string into array in php

PHP implode() function to concatenate the associative array values with multiple delimiters