Re: Adding a more logical string slicing function to PHP

From: Date: Fri, 01 Apr 2011 11:07:38 +0000
Subject: Re: Adding a more logical string slicing function to PHP
References: 1 2  Groups: php.internals 
Request: Send a blank email to internals+get-51786@lists.php.net to get a copy of this message
string substr( string $string, int $start [, int $second [, int $flag = SUBSTR_LENGTH ]])
I think this just makes code ugly. I think a new function with something like 'slice' in it is pretty unambiguous and unconfusing. (I also like the current substr() semantics. Actually, I think 'slice' is the new kid on the block, and has only really come up in the last 10 years or so as web tech and scripting languages have grown. Before that, in C and BASIC and so on, semantics like substr() has were more common--at least for positive numbers. But that's neither here nor there.) Ben. On 1/04/11 6:01 PM, Alban LEROUX wrote:
Hi all, I just come back on the first reflexion about adding a str_slice() function. As it says before, using one method or the orther can really sucks in some cases, depending what you are coding about. So actually you mention there is two possibility : - Keep all in the actual state. substr is allready quite cool but can suck in some case. - Add a new function and a create a possible confusion about the two method name (like javascript), etc... bad idea. In fact I see a third possibility, change the substr() function to handle the both case. Here a simple proto : string substr( string $string, int $start [, int $second [, int $flag = SUBSTR_LENGTH ]]) with flags : - SUBSTR_LENGTH (the default flags value corresponding to the actual behavior) - SUBSTR_OFFSET (the new possibility to use an offset in third argument) This keep the compatility with the actual substr() function, and enchance it, so why not ? On 2011-03-31 19:58:42 +0200, Martin Scotta said:
--20cf307d046c3ddbde049fcb0b8f Content-Type: text/plain; charset=ISO-8859-1 I think it's time to stop thinking in terms of "functions" and move forward to "abstractions" $s1 = 'string'; $s1->contains($s2); $s1->indexOf($s2) === strpos($s1, $s2); Why can't the strings be exposed as pseudo-objects ? users can choose to use them as a regular strings or by calling methods on it. Martin Scotta On Thu, Mar 31, 2011 at 12:52 PM, Rasmus Lerdorf <rasmus@lerdorf.com> wrote:
On 03/31/2011 08:45 AM, Philip Olson wrote:
- Intuitive name
Argh! Everyone should be forced to learn a bit of C. Like many PHP
functions, the name and argument order is right out of libc. If you type
"man strstr" at your (non-Windows) prompt you get a nice little
description of what it does.
-Rasmus
--20cf307d046c3ddbde049fcb0b8f--


« previous php.internals (#51786) next »