summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDavid Croft <david@php.net>2000-07-22 01:12:24 +0000
committerDavid Croft <david@php.net>2000-07-22 01:12:24 +0000
commit8dd1fdb1a0fc04c735e2b71471ab2ea4e45b8734 (patch)
tree03be7928a898c950dc891395ca4fce98bdf7a7f8
parente007639f2a24ed2647dbfe9879f613c73ed1d3bc (diff)
downloadphp-git-8dd1fdb1a0fc04c735e2b71471ab2ea4e45b8734.tar.gz
wordwrap function from Chris Russel <russel@yorku.ca>
differences from his patch: - wordwrap width and wrap-string now optional parameters (default to 75 and "\n" respectively) - wordwrap_byte is now just an automatic special case of wordwrap - Zend API compliant @- Added new function "wordwrap" to wordwrap long strings from Chris @ Russel <russel@yorku.ca> (David Croft)
-rw-r--r--ext/standard/basic_functions.c1
-rw-r--r--ext/standard/php_string.h1
-rw-r--r--ext/standard/string.c138
3 files changed, 140 insertions, 0 deletions
diff --git a/ext/standard/basic_functions.c b/ext/standard/basic_functions.c
index 6c0dafc4aa..29dbf82e26 100644
--- a/ext/standard/basic_functions.c
+++ b/ext/standard/basic_functions.c
@@ -106,6 +106,7 @@ function_entry basic_functions[] = {
PHP_FE(getimagesize, NULL)
+ PHP_FE(wordwrap, NULL)
PHP_FE(htmlspecialchars, NULL)
PHP_FE(htmlentities, NULL)
PHP_FE(get_html_translation_table, NULL)
diff --git a/ext/standard/php_string.h b/ext/standard/php_string.h
index 88b70f22c1..3046407d4c 100644
--- a/ext/standard/php_string.h
+++ b/ext/standard/php_string.h
@@ -46,6 +46,7 @@ PHP_FUNCTION(soundex);
PHP_FUNCTION(levenshtein);
PHP_FUNCTION(count_chars);
+PHP_FUNCTION(wordwrap);
PHP_FUNCTION(explode);
PHP_FUNCTION(implode);
PHP_FUNCTION(strtok);
diff --git a/ext/standard/string.c b/ext/standard/string.c
index c8217b8ace..cebd841138 100644
--- a/ext/standard/string.c
+++ b/ext/standard/string.c
@@ -226,6 +226,144 @@ PHP_FUNCTION(ltrim)
}
/* }}} */
+
+/* {{{ proto string wordwrap(string str[, int width[, string break]])
+ * 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;
+ char *text, *breakchar, *newtext;
+
+ if (ZEND_NUM_ARGS() < 1 || ZEND_NUM_ARGS() > 3 || zend_get_parameters_ex(ZEND_NUM_ARGS(), &ptext, &plinelength, &pbreakchar) == FAILURE) {
+ WRONG_PARAM_COUNT;
+ }
+
+ convert_to_string_ex(ptext);
+ text = (*ptext)->value.str.val;
+
+ if (ZEND_NUM_ARGS() > 1) {
+ convert_to_long_ex(plinelength);
+ linelength = (*plinelength)->value.lval;
+ }
+ else {
+ linelength = 75;
+ }
+
+ if (ZEND_NUM_ARGS() > 2) {
+ convert_to_string_ex(pbreakchar);
+ breakchar = (*pbreakchar)->value.str.val;
+ breakcharlen = (*pbreakchar)->value.str.len;
+ }
+ else {
+ breakchar = "\n";
+ breakcharlen = 1;
+ }
+
+ /* Special case for a single-character break as it needs no
+ additional storage space */
+
+ if (breakcharlen == 1) {
+
+ while (text[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') {
+ l --;
+ break;
+ }
+ l++;
+ }
+ if (l > linelength) {
+ pgr = l;
+ l = linelength;
+ /* needs breaking; work backwards to find previous word */
+ while (l >= 0) {
+ if (text[i+l] == ' ') {
+ text[i+l] = breakchar[0];
+ break;
+ }
+ l --;
+ }
+ if (l == -1) {
+ /* couldn't break is backwards, try looking forwards */
+ l = linelength;
+ while (l <= pgr) {
+ if(text[i+l] == ' ') {
+ text[i+l] = breakchar[0];
+ break;
+ }
+ l ++;
+ }
+ }
+ }
+ i += l+1;
+ }
+ RETVAL_STRINGL(text, strlen(text), 1);
+ }
+ else {
+ /* Multiple character line break */
+
+ newtext = emalloc((*ptext)->value.str.len * (breakcharlen+1));
+ newtext[0] = '\0';
+
+ i = 0;
+ while (text[i] != '\0') {
+ /* prescan line to see if it is greater than linelength */
+ l = 0;
+ while (text[i+l] != '\0') {
+ if (text[i+l] == breakchar[0]) {
+ if (breakcharlen == 1 || strncmp(text+i+l, breakchar, breakcharlen)==0)
+ break;
+ }
+ l ++;
+ }
+ if (l > linelength) {
+ pgr = l;
+ l = linelength;
+
+ /* needs breaking; work backwards to find previous word */
+ while (l >= 0) {
+ if (text[i+l] == ' ') {
+ strncat(newtext, text+last, i+l-last);
+ strcat(newtext, breakchar);
+ last = i + l + 1;
+ break;
+ }
+ l --;
+ }
+ if (l == -1) {
+ /* 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;
+ }
+ l ++;
+ }
+ }
+ i += l+1;
+ }
+ else {
+ i += (l ? l : 1);
+ }
+ }
+
+ if (i+l > last) {
+ strcat(newtext, text+last);
+ }
+
+ RETVAL_STRINGL(newtext, strlen(newtext), 1);
+ efree(newtext);
+ }
+}
+/* }}} */
+
+
PHPAPI void php_explode(zval *delim, zval *str, zval *return_value, int limit)
{
char *p1, *p2, *endp;