Re: [PHP4BETA] replacing a substring
| From: | Lars Torben Wilson | Date: | Wed, 17 Nov 1999 20:47:20 +0000 |
| Subject: | Re: [PHP4BETA] replacing a substring | ||
| References: | 1 2 3 4 5 6 | Groups: | php.version4 |
| Request: | Send a blank email to php-version4+get-6393@lists.php.net to get a copy of this message | ||
Andrei Zmievski writes:
> On Fri, 12 Nov 1999, Andrei Zmievski wrote:
> > At 06:45 PM 11/12/99 -0800, Lars Torben Wilson wrote:
> > >Ah. For this, I think you're stuck with just cutting up the string and
> > >replacing on the part you want, unless someone decides to add this
> > >functionality to PHP. Make a function out of it and stick it
> > >in a library file and you're all set.
> >
> > I'll see about making this possible.
>
> There is a new function for this now, substr_replace().
>
> -Andrei
Hi there,
May I propose a change in the argument order for substr_replace()? At
present, the argument list is:
substr_replace($string, $start [, $length [, $repl]])
The way it works now is effectively that if $length is not given,
neither may $repl be given, and nothing is done. It seems that this
order was chosen for its relationship to substr(). I think that it
might make more sense to go with a closer relationship to
str_replace(), and place the needle and the haystack closer
together. This would allow $length to be left out without affecting
$repl:
substr_replace($string, $repl [, $start [, $length]])
Now, with this idea, the only problem is what $length should default
to. Personally, I like it defaulting to 0, which just inserts $repl
into $string at $start. However, it would be more consistent with
substr() if $length defaulted to strlen( $string ), which would result
in everything from $start to the end of $string being replaced. Come
to think of if, perhaps strlen( $repl ) would make more sense.
At any rate, what do you think about the basic proposal? If you want
to try it out, diffs on string.c are attached at the end of this
message.
Torben
--- string.c.orig Wed Nov 17 04:24:29 1999
+++ string.c Wed Nov 17 12:17:32 1999
@@ -847,7 +847,7 @@
/* }}} */
-/* {{{ proto string substr_replace(string str, int start [, int length [, string repl]])
+/* {{{ proto string substr_replace(string str, string repl [, int start = 0 [, int length = 0]])
Replace part of a string with another string */
PHP_FUNCTION(substr_replace)
{
@@ -863,25 +863,28 @@
argc = ARG_COUNT(ht);
- if ((argc == 2 && getParametersEx(2, &string, &from) == FAILURE) ||
- (argc == 3 && getParametersEx(3, &string, &from, &len) == FAILURE) ||
- (argc == 4 && getParametersEx(4, &string, &from, &len, &repl) == FAILURE)
||
+ if ((argc == 2 && getParametersEx(2, &string, &repl) == FAILURE) ||
+ (argc == 3 && getParametersEx(3, &string, &repl, &from) == FAILURE) ||
+ (argc == 4 && getParametersEx(4, &string, &repl, &from, &len) == FAILURE)
||
argc < 2 || argc > 4) {
WRONG_PARAM_COUNT;
}
convert_to_string_ex(string);
- convert_to_long_ex(from);
- f = (*from)->value.lval;
+ convert_to_string_ex(repl);
if (argc == 2) {
- l = (*string)->value.str.len;
+ f = 0;
+ l = 0;
} else {
- convert_to_long_ex(len);
- l = (*len)->value.lval;
-
- if (argc == 4)
- convert_to_string_ex(repl);
+ convert_to_long_ex(from);
+ f = (*from)->value.lval;
+ if (argc == 3) {
+ l = 0;
+ } else {
+ convert_to_long_ex(len);
+ l = (*len)->value.lval;
+ }
}
/* if "from" position is negative, count start position from the end
@@ -912,15 +915,11 @@
l = (int)(*string)->value.str.len - f;
}
- if (argc == 4)
- result_len = (*string)->value.str.len - l + (*repl)->value.str.len;
- else
- result_len = (*string)->value.str.len - l;
- result = (char *)ecalloc(result_len + 1, sizeof(char *));
+ result_len = (*string)->value.str.len + (*repl)->value.str.len;
+ result = (char *)ecalloc(result_len, sizeof(char *));
strncat(result, (*string)->value.str.val, f);
- if (argc == 4)
- strcat(result, (*repl)->value.str.val);
+ strcat(result, (*repl)->value.str.val);
strcat(result, (*string)->value.str.val + f + l);
RETVAL_STRING(result, 0);