Portrety Uliczne Nieznajomych - zobacz wyjątkową galerię portretów z warszawskich ulic
ZALOGUJ SIĘ
login:
hasło:
przypomnij hasło
załóż konto użytkownika
(i zobacz kilka porad gratis)
   
WYSZUKIWARKA I DZIAŁY
całe porady  tytuły
zaznacz działy do przeszukania
(brak wyboru = wszystkie działy)
PHP
MySQL >
PostgreSQL
SQLite
Perl
Java
XML
XSLT
XPath
WML
SVG
RegExp
Wyszukiwarki
Ochrona
VBScript
Google Plus
XHTML/CSS
JavaScript
Grafika
Flash
Photoshop
Windows
Linux
Bash
Apache
Procmail
E-biznes
Explorer
Opera
Firefox
Inne porady
   
KURSY, DOKUMENTACJE
Własne:
XHTML/CSS
JavaScript
ActionScript
WML, RSS, SSI
Pozostałe:
PHP
MySQL
Java API
więcej...
   
użytkowników online: 53
W CZYM MOGĘ POMÓC?


   
OPINIE UŻYTKOWNIKÓW
Gratulacje i dzięki! Trafiłem tu przypadkiem poszukując informacji na temat php+mysql. Wiele polskich stron powiela identyczne przykłady, klonuje te same kursy i lekcje... ten serwis okazał sie inny. Zasada "problem - rozwiazanie - wyjaśnienie" zdaje egzamin - zapewnia jasną, jednoznaczną i pewną pomoc w konkretnym przypadku. Porady są warte swojej ceny, przede wszystkim ze względu na przyjazną (także dla początkujących) formę i treść oraz bogate i stale powiększane zasoby. Polecam i pozdrawiam!

Kamil Dmowski
Polski Czerwony Krzyż

   
GALERIA FOTOGRAFII
   
PODRĘCZNIK PHP 5.x, 4.x, 3.x - częściowo spolszczony / źródło: www.php.net

[Spis] [A] [B] [C] [D] [E] [F] [G] [H] [I] [J] [K] [L] [M] [N] [O] [P] [Q] [R] [S] [T] [U] [V] [X] [W] [Z]

wordwrap

(PHP 4 >= 4.0.2, PHP 5)

wordwrap --  Wraps a string to a given number of characters using a string break character

Description

string wordwrap ( string str [, int width [, string break [, bool cut]]] )

Returns a string with str wrapped at the column number specified by the optional width parameter. The line is broken using the (optional) break parameter.

wordwrap() will automatically wrap at column 75 and break using '\n' (newline) if width or break are not given.

If the cut is set to 1, the string is always wrapped at the specified width. So if you have a word that is larger than the given width, it is broken apart. (See second example).

Notatka: The optional cut parameter was added in PHP 4.0.3

Przykład 1. wordwrap() example

<?php
$text
= "The quick brown fox jumped over the lazy dog.";
$newtext = wordwrap($text, 20, "<br />\n");

echo
$newtext;
?>

This example would display:

The quick brown fox<br />
jumped over the lazy<br />
dog.

Przykład 2. wordwrap() example

<?php
$text
= "A very long woooooooooooord.";
$newtext = wordwrap($text, 8, "\n", 1);

echo
"$newtext\n";
?>

This example would display:

A very
long
wooooooo
ooooord.

See also nl2br() and chunk_split().




User Contributed Notes

lean[--]roico[at]gm[--]ail[dot]com
16-Jan-2006 05:37

To @Anthony Catel

Wouldn't be easier, more correct and more simple to cut the words having more characters than the $limit parameter like the following code?

<?
function cut_words( $txt , $limit, $html_nl = 0 )
{
  
$str_nl = $html_nl == 1 ? "<br>" : ( $html_nl == 2 ? "<br />" : "\n" );
  
$pseudo_words = explode( ' ',$txt );
  
$txt = '';
   foreach(
$pseudo_words as $v )
   {
       if( (
$tmp_len = strlen( $v ) ) > $limit )
       {
          
$final_nl = is_int( $tmp_len / $limit );
          
$txt .= chunk_split( $v, $limit, $str_nl );
           if( !
$final_nl )
              
$txt = substr( $txt, 0, - strlen( $str_nl ) );
          
$txt .= ' ';
       }
       else
          
$txt .= $v . ' ';
   }
   return
substr( $txt, 0 , -1 );
}
?>

For using it this is a sample:
<?
function cut_words( 'big words text here' , 15, 2 );
?>


Anthony Catel (paraboul at gmail . com)
15-Jan-2006 10:56

<?php
  
function cut_word($txt, $where) {
       if (empty(
$txt)) return false;
       for (
$c = 0, $a = 0, $g = 0; $c<strlen($txt); $c++) {
          
$d[$c+$g]=$txt[$c];
           if (
$txt[$c]!=" ") $a++;
           else if (
$txt[$c]==" ") $a = 0;
           if (
$a==$where) {
          
$g++;
          
$d[$c+$g]="<br />";
          
$a = 0;
           }
       }
       return
implode("", $d);
   }
?>

Wrap word for cut only long word :
<?
   cut_word
($sentence, 25);
?>


matyx666 at yahoo dot com dot ar
10-Jan-2006 05:19

This function wraps a string with the characters count specified:

   static function cortarCaracteres($sText, $iCharsCount, $sCutWith = "...") {
       $sTextLength = strlen($sText);

       if($sTextLength <= $iCharsCount) {
           return $sText;
       }

       if($sTextLength+strlen($sCutWith) > $iCharsCount) {
           return substr($sText, 0, $iCharsCount-strlen($sCutWith)).$sCutWith;
       }

       $iLeftOverChars = strlen($sCutWith) - ($sTextLength - $iCharsCount);

       return substr($sText, 0, $sTextLength-$iLeftOverChars).$sCutWith;
   }

Regards!


Dave Lozier - dave at fusionbb.com
30-Nov-2005 09:48

If you'd like to break long strings of text but avoid breaking html you may find this useful. It seems to be working for me, hope it works for you. Enjoy. :)

   function textWrap($text) {
       $new_text = '';
       $text_1 = explode('>',$text);
       $sizeof = sizeof($text_1);
       for ($i=0; $i<$sizeof; ++$i) {
           $text_2 = explode('<',$text_1[$i]);
           if (!empty($text_2[0])) {
               $new_text .= preg_replace('#([^\n\r .]{25})#i', '\\1  ', $text_2[0]);
           }
           if (!empty($text_2[1])) {
               $new_text .= '<' . $text_2[1] . '>';   
           }
       }
       return $new_text;
   }


frans-jan at van-steenbeek dot R-E-M-O-V-E dot net
16-Nov-2005 03:17

Using wordwrap is usefull for formatting email-messages, but it has a disadvantage: line-breaks are often treated as whitespaces, resulting in odd behaviour including lines wrapped after just one word.

To work around it I use this:

<?php
 
function linewrap($string, $width, $break, $cut) {
 
$array = explode("\n", $string);
 
$string = "";
  foreach(
$array as $key => $val) {
  
$string .= wordwrap($val, $width, $break, $cut);
  
$string .= "\n";
  }
  return
$string;
 }
?>

I then use linewrap() instead of wordwrap()

hope this helps someone


tjomi4 at yeap dot lv
23-Sep-2005 12:26

utf8_wordwrap();

usage: utf8_wordwrap("text",3,"<br>");
coded by tjomi4`, thanks to SiMM.
web: www.yeap.lv

<?php

function utf8_wordwrap($str,$len,$what){
# usage: utf8_wordwrap("text",3,"<br>");
# by tjomi4`, thanks to SiMM.
# www.yeap.lv
$from=0;
$str_length = preg_match_all('/[\x00-\x7F\xC0-\xFD]/', $str, $var_empty);
$while_what = $str_length / $len;
while(
$i <= round($while_what)){
$string = preg_replace('#^(?:[\x00-\x7F]|[\xC0-\xFF][\x80-\xBF]+){0,'.$from.'}'.
                      
'((?:[\x00-\x7F]|[\xC0-\xFF][\x80-\xBF]+){0,'.$len.'}).*#s',
                      
'$1',$str);
$total .= $string.$what;
$from = $from+$len;
$i++;
}
return
$total;
}
?>


vex
20-Aug-2005 08:19

Useful in chat's, shoutboxes sth. like that (simply written):

$text = explode(" ", $text);
$l=count($text);
$counter=0;

while($counter<=$l) {
$text[$counter] = wordwrap($text[$counter], 20, " ", 1);
$counter++;
}

$text=implode(" ", $text);


pawan at shopsbs dot com
14-Aug-2005 07:32

I wanted to use this function to add a particular text after a certain word count. Here is how I implemented it:

$content = wordwrap($content, 200, "....<br /><!--more-->\n");

Above code adds the text '...<br/><!--more-->\n' after a 200 word count. I know there are a million ways this can be done in PHP. Go PHP!


x403 at yandex dot ru
23-Jun-2005 03:35

String lenght control:

if (str_word_count($STRING) > 0)
   $div = strlen($STRING) / str_word_count($STRING);
else return " ";

if ( $div > 25 ) return wordwrap($STRING, 25, "\n", 1);

return $STRING;

// Maximum word lenght is 25 chars


ajd at cloudiness dot com
13-Jun-2005 11:13

Kyle's preserve_wordwrap is handy after fixing this error:

     $strs = explode($tstr,$br);
should be
     $strs = explode($br,$tstr);


Kyle
31-May-2005 08:34

Yet-another-wordwrap-improvement... If you attempt to wordwrap() lines that already contain some line-breaks that you want to maintain, a simple wrapper around wordwrap can help. For example:

function preserve_wordwrap($tstr, $len = 75, $br = '\n') {
     $strs = explode($tstr,$br);
     $retstr = "";
     foreach ($strs as $str) {
         $retstr .= wordwrap($str,$len,$br) . $br;
     }
     return $retstr;
}

I used a function like this for pulling quotes for my email out of a mysql database and formatting them for use in an email. Some quotes had multiple lines (e.g. a short conversation-style quote) that I wanted to maintain yet still word-wrap correctly.


cdblog at gmail dot com
21-May-2005 07:36

note:
if you can't see the example, please change your browser's default encode to Chinese Simplified (GB2312). :)
also, this function maybe will work at other double-bytes language, such as Korean.

<?php
/**
* @return string
* @author Cocol
* @desc the wordwrap for Chinese ,Janpanese
* for more detail please visit http://php.clickz.cn/articles/string/wrodwrap.html
*/
function wordwrap_cn($str = "",$len = 80,$glue = "\n") {
for (
$i=0;$i<strlen($str);$i++) {
if (
ord(substr($str,$i,1)) > 128) {
$str_arr[] = substr($str,$i,2);
$i++;
} else {
$str_arr[] = substr($str,$i,1);
}
}
$tmp = array_chunk($str_arr,$len);
$str = "";
foreach (
$tmp as $key=>$val) {
$str .= implode("",$val).$glue;
}
return
$str;
}
//example 1: Chinese test
$str = "

 

 
  © 1996-2012 & Reporter.plmiejscao serwisieabonamentwarunki korzystaniaRSSkontakt