1 3 Votes

PHP: strStart and strEnd functions

Tip by NetGuy | 29/03/2014 at 20:10

When programming in PHP, often, I am missing a simple function to check whether a string is starting or ending with a specific string or character. In other words, something like strStart or strEnd well known from other programming languages.

Of course, you can solve this problem by using substr() or strpos(), but it is much more easier if you have the following functions:

function strStart($s, $search) {
  return substr($s, 0, strlen($search)) === $search; 

function strEnd($s, $search) {
  return substr($s, -strlen($search)) === $search;

A call of this functions can look like this, for example:

$s = "abcdef";

if (strStart($s, "abc")) {  }  // true
if (strEnd($s, "def"))   {  }  // true

As a first paramater you are passing the string you would like to check, as a second parameter you are passing the search string.


About the Author

Avatar AuthorThe author has not added a short description to his profile yet.
Show Profile | Message


Related Topics

Important Note

Please note: The contributions published on askingbox.com are contributions of users and should not substitute professional advice. They are not verified by independents and do not necessarily reflect the opinion of askingbox.com. Learn more.


Ask your own question or write your own articles on askingbox.com. How to do.