Missing PHP String Functions: contains, equals, startsWith, endsWith

PHP is great because it is easy to deploy it on pretty much any webserver. But in my opinion the language itself is not very well designed. In this post I will take a look at the PHP string functions and offer implementations of those important string functions that PHP is missing (which are endsWith, startsWith, contains and equals).

PHP and Java String functions compared: A Rant

If you look at the Java String class it has all methods that one commonly needs for strings, and the method signatures are well designed. Even without remembering the exact names, it is easy to guess the method names as well as the arguments.

Now take a look at the PHP String functions. Some function names start with “str” (e.g. strpos), some end with “str” (e.g. substr), and some do not contain “str” at all (e.g. strip_tags). Some use an underscore to seperate (e.g. str_replace) and some do not (e.g. strnatcmp). And many are abbreviated in a way that results in an awful method name. I can maybe guess what strrev is supposed to do, but what about stristr, strpbrk or strchr (which is only an alias for strstr). We measure hard disks in gigabyte and terabyte, it is ok to waste an extra bit or two for a decent function name.

So I think that it is fair to say that the naming convention for PHP String functions is non-existent and that one either has to remember every method name or search for it each time it is used.

As if that wasn’t enough, some of the most important functions such as equal and contains do not even exist in PHP.

Important Java string functions to PHP

The string functions contains, equals, endsWith, startsWith, and – to a lesser extend – charAt are vital when working with strings, but they are missing from PHP. So here they are:

// does $big_string contain $small_string ?
// the empty string is contained by all strings.
function contains($big_string, $small_string) {
    if (strlen($small_string) == 0) {return true;}
    return (strpos($big_string, $small_string) !== false);

// are the given strings equal (case-sensitive)?
function equals($string1, $string2) {
    return !strcmp($string1, $string2);

// does $big_string start with $small_string?
// every string starts with the empty string
function startsWith($big_string, $small_string) {
    return !strncmp($big_string, $small_string, strlen($small_string));

// does $big_string end with $small_string?
// every string ends with the empty string
function endsWith($big_string, $small_string) {
    if (strlen($small_string) == 0) {return true;}
    return (substr($big_string, -strlen($small_string)) === $small_string);

// returns character at the given $position of $php_string
// or empty string if $position is outside of the string.
function charAt($php_string, $position) {
    if ($position < 0 || strlen($php_string) <= $position) {return '';}
    return $php_string[$position];

And just for reference, here are five often used PHP string functions and their Java equivalent. I’m thinking of collecting some more and wrapping them up in coherently named function names, but I could also try to remember 5 function names :)

build-in php string functions (equivalent Java methods added in "[]"):

int strlen ( string $string ) [length] - returns length of string

string strtoupper(string $string) [toUpperCase] - returns given string to all upper-case

mixed str_replace(mixed $search_string, mixed $replace, mixed $subject) [replace] -
replaces given $search_string in $subject with $replace.

int strrpos ($big_string, $small_string) [lastIndexOf] -
returns the position of the LAST(!) occurrence of $small_string in $big_string

string substr ( string $string , int $start [, int $length ] ) [substring] -
returns a substring of the given string. first character is position 0, as it is in Java. BUT PHP expects the length of the substring as second parameter, while Java expects an index instead.

If there are any important PHP string functions that I missed, please feel free to add them in the comments!

Related Posts:

Leave a Reply

Your email address will not be published.