For the start of the string:

  substr($haystack,0,strlen($needle)) == $needle

And for the end of the string:

  substr($haystack,-strlen($needle)) == $needle

For case-insensitivity, just strtolower both.

Writing built-in functions for something that can be done with trivial one-liners isn't something we typically do.

-Rasmus

Lars Strojny wrote:
Hi Martin,

first of all, thanks for you work! A few comments below.

Am Samstag, den 19.07.2008, 14:55 +0200 schrieb Martin Jansen:
Attached you'll find a patch against PHP_5_3 that implements two new
string functions:

str_startswith(haystack, needle [, case_sensitivity])

That's in my opinion too easy to solve with strpos($haystack, $needle)
=== 0.

checks if haystack starts with needle. The check is performed
case-insensitively, but this can be overridden by passing TRUE as the
value for the third parameter. The second function

str_endswith(haystack, needle [, case_sensitivity])
[...]

Suffix checking is indeed a bit harder. But I'm not sure if we should
introduce a function for every special case. Suffix verification often
is about verifying file extensions. That could be easily solved with
pathinfo() (and to make that easier, it would be more helpful to allow
the engine to directly access returned arrays like
pathinfo($file)['extension']). The other thing I would love to see is
something I really love in Ruby: you have various ways to work with
substrings[1]. For example you can do "foobar"[-3,3] == "bar" to check
the suffix. That's in my opinion much easier and flexible.

So -1 from me for the new string functions, but +1 for niftier variants
to work with offsets.

[1] http://pleac.sourceforge.net/pleac_ruby/strings.html

cu, Lars


--
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to