There are only two hard things in Computer Science: cache invalidation and naming things.
— Phil Karlton
I wanted to grab the last bit of a url that I knew would be the name of an image. I knew strstr well but that operates by giving you the remainder of a string that occurs after some needle in a string haystack. I wanted this behaviour, but only from the last instance of the needle.
strstr — Returns part of haystack string starting from and including the first occurrence of needle to the end of haystack.
strrchr -This function returns the portion of haystack which starts at the last occurrence of needle and goes until the end of haystack.
Let’s look at how these work with an example
$url = 'http://www.google.com/a/b/c/d,img'; echo strrchr($url, '/'); // prints /d.img echo strstr($url, '/'); // prints //www.google.com/a/b/c/d.img
Now I’ve been programming in PHP for pushing on 12 years and this one still did my head in. The names of two very similar behaving functions bare little resemblance to each other.
At this point the arguments and criticisms over the core API have been exhausted and there’s little that can/will be done. But I do wonder if it would be worth creating an object library to encapsulate primitive functions such as String, Integer, Array, Float etc., I’m not sure how possible auto-boxing is with PHP, and indeed if it’s even a good idea. But definitely some object wrappers would help ease this API pain.