summaryrefslogtreecommitdiff
path: root/ext/standard/string.c
diff options
context:
space:
mode:
authorDerick Rethans <derick@php.net>2000-09-11 20:06:24 +0000
committerDerick Rethans <derick@php.net>2000-09-11 20:06:24 +0000
commit71066c12dc847da45a4a8e6eb0ea105ca79c83f6 (patch)
tree428734406457105cc732027b869b0016f5b00c66 /ext/standard/string.c
parentf5cf7d7eb752e04e02799a652507481f74db32b6 (diff)
downloadphp-git-71066c12dc847da45a4a8e6eb0ea105ca79c83f6.tar.gz
- Fix for bug #6673
- Added a "cut" option to wordwrap (as per feature request #6429) @ Added an optional parameter to wordwrap that cuts a string if the length of a word is longer than the maximum allowed with (Derick)
Diffstat (limited to 'ext/standard/string.c')
-rw-r--r--ext/standard/string.c57
1 files changed, 37 insertions, 20 deletions
diff --git a/ext/standard/string.c b/ext/standard/string.c
index 71e188b68b..8274ea806c 100644
--- a/ext/standard/string.c
+++ b/ext/standard/string.c
@@ -233,15 +233,15 @@ PHP_FUNCTION(ltrim)
/* }}} */
-/* {{{ proto string wordwrap(string str [, int width [, string break]])
+/* {{{ proto string wordwrap(string str [, int width [, string break [, int cut]]])
Wrap buffer to selected number of characters using string break char */
PHP_FUNCTION(wordwrap)
{
- pval **ptext, **plinelength, **pbreakchar;
- long i=0, l=0, pgr=0, linelength=0, last=0, breakcharlen;
+ pval **ptext, **plinelength, **pbreakchar, **cut;
+ long i=0, l=0, pgr=0, linelength=0, last=0, breakcharlen, docut=0;
char *text, *breakchar, *newtext;
- if (ZEND_NUM_ARGS() < 1 || ZEND_NUM_ARGS() > 3 || zend_get_parameters_ex(ZEND_NUM_ARGS(), &ptext, &plinelength, &pbreakchar) == FAILURE) {
+ if (ZEND_NUM_ARGS() < 1 || ZEND_NUM_ARGS() > 4 || zend_get_parameters_ex(ZEND_NUM_ARGS(), &ptext, &plinelength, &pbreakchar, &cut) == FAILURE) {
WRONG_PARAM_COUNT;
}
@@ -266,16 +266,21 @@ PHP_FUNCTION(wordwrap)
breakcharlen = 1;
}
+ if (ZEND_NUM_ARGS() > 3) {
+ convert_to_long_ex(cut);
+ docut = (*cut)->value.lval;
+ }
+
/* Special case for a single-character break as it needs no
additional storage space */
- if (breakcharlen == 1) {
-
- while (text[i] != '\0') {
+ if (breakcharlen == 1 && docut == 0) {
+ newtext = estrdup (text);
+ while (newtext[i] != '\0') {
/* prescan line to see if it is greater than linelength */
l = 0;
- while (text[i+l] != breakchar[0]) {
- if (text[i+l] == '\0') {
+ while (newtext[i+l] != breakchar[0]) {
+ if (newtext[i+l] == '\0') {
l --;
break;
}
@@ -286,8 +291,8 @@ PHP_FUNCTION(wordwrap)
l = linelength;
/* needs breaking; work backwards to find previous word */
while (l >= 0) {
- if (text[i+l] == ' ') {
- text[i+l] = breakchar[0];
+ if (newtext[i+l] == ' ') {
+ newtext[i+l] = breakchar[0];
break;
}
l --;
@@ -296,8 +301,8 @@ PHP_FUNCTION(wordwrap)
/* couldn't break is backwards, try looking forwards */
l = linelength;
while (l <= pgr) {
- if(text[i+l] == ' ') {
- text[i+l] = breakchar[0];
+ if(newtext[i+l] == ' ') {
+ newtext[i+l] = breakchar[0];
break;
}
l ++;
@@ -306,11 +311,11 @@ PHP_FUNCTION(wordwrap)
}
i += l+1;
}
- RETVAL_STRINGL(text, strlen(text), 1);
+ RETVAL_STRINGL(newtext, strlen(newtext), 1);
+ efree(newtext);
}
else {
/* Multiple character line break */
-
newtext = emalloc((*ptext)->value.str.len * (breakcharlen+1));
newtext[0] = '\0';
@@ -343,11 +348,23 @@ PHP_FUNCTION(wordwrap)
/* couldn't break it backwards, try looking forwards */
l = linelength;
while (l <= pgr) {
- if (text[i+l] == ' ') {
- strncat(newtext, text+last, i+l-last);
- strcat(newtext, breakchar);
- last = i + l + 1;
- break;
+ if (docut == 0)
+ {
+ if (text[i+l] == ' ') {
+ strncat(newtext, text+last, i+l-last);
+ strcat(newtext, breakchar);
+ last = i + l + 1;
+ break;
+ }
+ }
+ if (docut == 1)
+ {
+ if (text[i+l] == ' ' || l > i-last) {
+ strncat(newtext, text+last, i+l-last);
+ strcat(newtext, breakchar);
+ last = i + l;
+ break;
+ }
}
l ++;
}