cvs: /php4/ext/standard basic_functions.c php3_string.h string.c
| From: | Andrei Zmievski | Date: | Tue, 16 Nov 1999 22:44:45 +0000 |
| Subject: | cvs: /php4/ext/standard basic_functions.c php3_string.h string.c | ||
| Groups: | php.version4 | ||
| Request: | Send a blank email to php-version4+get-6334@lists.php.net to get a copy of this message | ||
andrei Tue Nov 16 17:44:45 1999 EDT
Modified files:
/php4/ext/standard basic_functions.c php3_string.h string.c
Log:
(PHP substr_replace) New function.
Index: php4/ext/standard/basic_functions.c
diff -u php4/ext/standard/basic_functions.c:1.121 php4/ext/standard/basic_functions.c:1.122
--- php4/ext/standard/basic_functions.c:1.121 Sat Nov 13 15:32:49 1999
+++ php4/ext/standard/basic_functions.c Tue Nov 16 17:44:13 1999
@@ -134,6 +134,7 @@
PHP_FE(stristr, NULL)
PHP_FE(strrchr, NULL)
PHP_FE(substr, NULL)
+ PHP_FE(substr_replace, NULL)
PHP_FE(quotemeta, NULL)
PHP_FE(ucfirst, NULL)
PHP_FE(ucwords, NULL)
Index: php4/ext/standard/php3_string.h
diff -u php4/ext/standard/php3_string.h:1.19 php4/ext/standard/php3_string.h:1.20
--- php4/ext/standard/php3_string.h:1.19 Wed Oct 27 18:06:05 1999
+++ php4/ext/standard/php3_string.h Tue Nov 16 17:44:13 1999
@@ -29,7 +29,7 @@
*/
-/* $Id: php3_string.h,v 1.19 1999/10/27 22:06:05 andrei Exp $ */
+/* $Id: php3_string.h,v 1.20 1999/11/16 22:44:13 andrei Exp $ */
/* Synced with php3 revision 1.43 1999-06-16 [ssb] */
@@ -85,6 +85,7 @@
PHP_FUNCTION(similar_text);
PHP_FUNCTION(strip_tags);
PHP_FUNCTION(str_repeat);
+PHP_FUNCTION(substr_replace);
extern PHPAPI char *php_strtoupper(char *s);
extern PHPAPI char *php_strtolower(char *s);
Index: php4/ext/standard/string.c
diff -u php4/ext/standard/string.c:1.55 php4/ext/standard/string.c:1.56
--- php4/ext/standard/string.c:1.55 Sun Nov 14 12:20:56 1999
+++ php4/ext/standard/string.c Tue Nov 16 17:44:13 1999
@@ -18,7 +18,7 @@
+----------------------------------------------------------------------+
*/
-/* $Id: string.c,v 1.55 1999/11/14 17:20:56 thies Exp $ */
+/* $Id: string.c,v 1.56 1999/11/16 22:44:13 andrei Exp $ */
/* Synced with php3 revision 1.193 1999-06-16 [ssb] */
@@ -845,6 +845,88 @@
RETVAL_STRINGL((*string)->value.str.val + f, l, 1);
}
/* }}} */
+
+
+/* {{{ proto string substr_replace(string str, int start [, int length [, string repl]])
+ Replace part of a string with another string */
+PHP_FUNCTION(substr_replace)
+{
+ zval** string;
+ zval** from;
+ zval** len;
+ zval** repl;
+ char* result;
+ int result_len;
+ int argc;
+ int l;
+ int f;
+
+ 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)
||
+ argc < 2 || argc > 4) {
+ WRONG_PARAM_COUNT;
+ }
+
+ convert_to_string_ex(string);
+ convert_to_long_ex(from);
+ f = (*from)->value.lval;
+
+ if (argc == 2) {
+ l = (*string)->value.str.len;
+ } else {
+ convert_to_long_ex(len);
+ l = (*len)->value.lval;
+
+ if (argc == 4)
+ convert_to_string_ex(repl);
+ }
+
+ /* if "from" position is negative, count start position from the end
+ * of the string
+ */
+ if (f < 0) {
+ f = (*string)->value.str.len + f;
+ if (f < 0) {
+ f = 0;
+ }
+ }
+
+ /* if "length" position is negative, set it to the length
+ * needed to stop that many chars from the end of the string
+ */
+ if (l < 0) {
+ l = ((*string)->value.str.len - f) + l;
+ if (l < 0) {
+ l = 0;
+ }
+ }
+
+ if (f >= (int)(*string)->value.str.len) {
+ RETURN_STRINGL((*string)->value.str.val, (*string)->value.str.len, 1);
+ }
+
+ if((f+l) > (int)(*string)->value.str.len) {
+ 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 *));
+
+ strncat(result, (*string)->value.str.val, f);
+ if (argc == 4)
+ strcat(result, (*repl)->value.str.val);
+ strcat(result, (*string)->value.str.val + f + l);
+
+ RETVAL_STRING(result, 0);
+}
+/* }}} */
+
/* {{{ proto string quotemeta(string str)
Quote meta characters */