Thursday, September 18, 2008

The wordwrap() function

This function wraps a string into a new line when it reaches a specified length.

Syntax: wordwrap(string[,length,break,cut]);

length – the number of characters after which the string is wrapped. The default length is 75.

break – the character to be used as a separator. “\n” is the default.

cut – If this is TRUE, words longer than the specified length will also be wrapped.

If this value is FALSE, words longer than the specified length will not be wrapped. FALSE is the default value.

Example inputs:

1.
$str1="This is a simple PHP program";
echo $str1;
echo "
";
$str2 = wordwrap($str1,2,"
",true);
echo $str2;

2.
$str1="This is a simple PHP program";
echo $str1;
echo "
";
$str2 = wordwrap($str1,2,"
");
echo $str2;

In the second example, we have not mentioned any value for the cut parameter. The default value is FALSE and hence, words in the string that are longer than the specified length(here 2) will not be wrapped.

Example outputs:

1.
This is a simple PHP program Th is is a si mp le PH P pr og ra m

2.
This is a simple PHP program This is a simple PHP program

No comments: