<?php
str wordwrap ( str $str [,
int $width = 75 [,
str $break = "\n" [,
bool $cut = FALSE ]]] )
where,
$str = The input STRING to wrap
$width = The number of characters at which the
STRING will be wrapped
$break = The character to wordwrap the STRING
$cut = To control the wrap before or
after the specified width
?>
VALUE | MEANING |
FALSE | This function does not split the word even if the width is smaller than the word width. |
TRUE | The STRING will be wrapped at or before the specified width. If there is a word that is larger than the given width, it is broken apart. |
ed48 |
<?php
$str01 = 'United we stand, divided we fall.';
$width01 = mt_rand(1, strlen($str01));
$break01 = '<br>';
$cut01 = FALSE;
echo '"'. $str01 . '"<br>';
echo strlen($str01) . ' characters<br><br>';
echo 'Width = ' . $width01 . '<br>Break = "<br>"<br>';
echo 'Cut = ';
var_dump($cut01);
echo '<br><br>';
echo wordwrap($str01,
$width01,
$break01,
$cut01) . '<br><br>';
?>
<?php
$str02 = 'United we stand, divided we fall.';
$width02 = mt_rand(1, strlen($str02));
$break02 = '<br>';
$cut02 = TRUE;
echo '"'. $str02 . '"<br>';
echo strlen($str02) . ' characters<br><br>';
echo 'Width = ' . $width02 . '<br>Break = "<br>"<br>';
echo 'Cut = ';
var_dump($cut02);
echo '<br><br>';
echo wordwrap($str02,
$width02,
$break02,
$cut02) . '<br><br>';
?>
<?php
$str03 = 'United we stand, divided we fall.';
$width03 = 5;
$break03 = '<br>';
$cut03 = TRUE;
echo '"'. $str03 . '"<br>';
echo strlen($str03) . ' characters<br><br>';
echo 'Width = ' . $width03 . '<br>Break = "<br>"<br>';
echo 'Cut = ';
var_dump($cut03);
echo '<br><br>';
$nwt01 = wordwrap($str03, $width03, $break03, $cut03);
echo $nwt01 . '<br><br>'
?>
<?php
$str04 = 'United we stand, divided we fall.';
$width04 = 5;
$break04 = '#@#';
$cut04 = FALSE;
echo '"'. $str04 . '"<br>';
echo strlen($str04) . ' characters<br><br>';
echo "Width = $width04<br>Break = $break04<br>";
echo 'Cut = ';
var_dump($cut04);
echo '<br><br>';
$nwt04 = wordwrap($str04, $width04, $break04, $cut04);
echo $nwt04 . '<br><br>'
?>
<?php
echo "Testing wordwrap() : basic functionality.<br><br>";
// Initialize all required variables
$str = "The quick brown foooooooooox
jummmmmmmmmmmmped over the lazzzzzzzzzzzy
doooooooooooooooooooooog.";
$width = 80;
$break = "<br>";
echo "Given:<br>$str<br><br>\$width = $width<br>\$break = \"<br>\"<br><br><br>";
// Calling wordwrap() with default arguments
echo "After:<br>wordwrap(\"$str\")<br><br>";
var_dump( wordwrap($str) );
echo "<br><br>After:<br>wordwrap(\"$str\", \"$width\")<br><br>";
// Calling wordwrap() with all possible optional arguments
// with $width arg
var_dump( wordwrap($str, $width) );
// with $break arg
echo "<br><br>After:<br>wordwrap(\"$str\", \"$width\", \"$break\")<br><br>";
var_dump( wordwrap($str, $width, $break) );
// Calling wordwrap() with all arguments
// $cut as true
$width = 10;
$cut = true;
echo "<br><br><br>Given:<br>\$width = $width<br>\$cut = $cut<br><br><br>";
echo "After:<br>wordwrap(\"$str\", \"$width\", \"$break\", \"$cut\")<br><br>";
var_dump( wordwrap($str, $width, $break, $cut) );
// $cut as false
$width = 10;
$cut = false;
echo "<br><br><br>Given:<br>\$width = $width<br>\$cut = $cut<br><br><br>";
echo "After:<br>wordwrap(\"$str\", \"$width\", \"$break\", \"$cut\")<br><br>";
var_dump( wordwrap($str, $width, $break, $cut) );
?>
<?php
echo "Testing wordwrap() : error conditions.<br>";
$str = 'testing wordwrap function';
$width = 10;
$break = '<br>';
$cut = true;
// $width arg as negative value
echo "<br><br>Testing wordwrap() function
with negative/zero value
for width argument.<br>";
echo "<br><br>width = 0 & cut = false:<br>";
// width as zero and cut as false
$width = 0;
$cut = false;
var_dump( wordwrap($str, $width, $break, $cut) );
echo "<br><br>width = 0 & cut = true:<br>";
// width as zero and cut as true
$width = 0;
$cut = true;
try {
wordwrap($str, $width, $break, $cut);
} catch (\ValueError $e) {
echo $e->getMessage() . "<br>";
}
echo "<br><br>width = -10 & cut = false<br>";
// width as -ne and cut as false
$width = -10;
$cut = false;
var_dump( wordwrap($str, $width, $break, $cut) );
echo "<br><br>width = -10 & cut = true:<br>";
// width as -ne and cut as true
$width = -10;
$cut = true;
var_dump( wordwrap($str, $width, $break, $cut) );
?>
<?php
$str = str_repeat('x', 65534);
$str2 = str_repeat('x', 65535);
wordwrap($str, 1, $str2);
?>
<?php
/*
* Test wordwrap() with break arguments
* as single spaces
*/
echo "Testing wordwrap() : usage variations.";
// Initialize all required variables
$str = "Testing wordrap function";
$width = 1;
$cut = false;
echo "<br><br><br>Given:<br>\$str = $str<br>\$width = $width<br>\$cut = $cut<br><br><br>";
echo "Testing wordwrap()
with default break value
and single space as value.<br>";
echo "<br><br>1. with default break and cut value:<br>";
var_dump( wordwrap($str, $width) );
// default break and cut value
echo "<br><br>2. with default cut value:<br>";
$break = ' ';
$break1 = " ";
echo "<br>Given:<br>\$str = $str<br>\$width = $width<br>\$break = $break<br><br><br>";
var_dump( wordwrap($str, $width, $break) );
echo "<br><br><br>Given:<br>\$str = $str
<br>\$width = $width<br>\$break1 = $break1<br>";
var_dump( wordwrap($str, $width, $break1) );
echo "<br><br>3. with cut value as false:";
$cut = false;
echo "<br><br>Given:<br>\$str = $str<br>
\$width = $width<br>\$break = $break<br>\$cut = $cut<br><br>";
var_dump( wordwrap($str, $width, $break, $cut) );
echo "<br><br><br>Given:<br>\$str = $str<br>\$width = $width
<br>\$break1 = $break1<br>\$cut = $cut<br><br>";
var_dump( wordwrap($str, $width, $break1, $cut) );
echo "<br><br>4. with cut value as true:<br>";
$cut = true;
echo "<br>Given:<br>\$str = $str<br>\$width = $width
<br>\$break = $break<br>\$cut = $cut<br><br>";
var_dump( wordwrap($str, $width, $break, $cut) );
echo "<br><br><br>Given:<br>\$str = $str<br>\$width = $width
<br>\$break1 = $break1<br>\$cut = $cut<br><br>";
var_dump( wordwrap($str, $width, $break1, $cut) );
?>