- 6 years ago
- Zaid Bin Khalid
- 10,080 Views
-
8
PHP is a proper language it has many built-in functions that we can use to do that or we can create our own function. So, technically PHP cover all the things that we need.
We are going to break the string with N number for that we use wordwrap(). It is a PHP built-in function that we use to break a string. And this function will return the string. You can convert it into an array.
wordwrap ( string $str [, int $width = 75 [, string $break = "\n" [, bool $cut = FALSE ]]] ) : string
In above syntax wordwrap function has four parameters.
- The String is the input string.
- A Width is a number of characters at which the string will be wrapped.
- The line breaks with optional Break parameter.
- If the Cut is set to,
TRUE
the string is always wrapped at or before the specified Width. So if you have a word that is larger than the given width, it is broken apart. When the FALSE
function does not split the word even if the is Width smaller than the word width.
//This function is used to create EXCEL
function breakStr($string,$length){
$stringAry = explode("||",wordwrap($string, $length, "||"));
foreach($stringAry as $key=>$val){
print '['.$key.'] Character length <strong>('.strlen($val).')</strong><br />';
}
return $stringAry;
}
$string = "Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.";
$rowVal = breakStr($string,48);
print"<pre>";
print_r($rowVal);
print"</pre>";
The output will be.
[0] Character length (48)
[1] Character length (46)
[2] Character length (45)
[3] Character length (48)
[4] Character length (48)
[5] Character length (46)
[6] Character length (46)
[7] Character length (39)
[8] Character length (44)
[9] Character length (48)
[10] Character length (41)
[11] Character length (48)
[12] Character length (15)
Array
(
[0] => Lorem Ipsum is simply dummy text of the printing
[1] => and typesetting industry. Lorem Ipsum has been
[2] => the industry's standard dummy text ever since
[3] => the 1500s, when an unknown printer took a galley
[4] => of type and scrambled it to make a type specimen
[5] => book. It has survived not only five centuries,
[6] => but also the leap into electronic typesetting,
[7] => remaining essentially unchanged. It was
[8] => popularised in the 1960s with the release of
[9] => Letraset sheets containing Lorem Ipsum passages,
[10] => and more recently with desktop publishing
[11] => software like Aldus PageMaker including versions
[12] => of Lorem Ipsum.
)
Note: We convert a string into an array with explode function. I do not use explode function then the output will be a string as shown in below example.
$string = "Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.";
$rowVal = wordwrap($string, 48, "<br />");
print"<pre>";
print_r($rowVal);
print"</pre>";
The output will be.
Lorem Ipsum is simply dummy text of the printing
and typesetting industry. Lorem Ipsum has been
the industry's standard dummy text ever since
the 1500s, when an unknown printer took a galley
of type and scrambled it to make a type specimen
book. It has survived not only five centuries,
but also the leap into electronic typesetting,
remaining essentially unchanged. It was
popularised in the 1960s with the release of
Letraset sheets containing Lorem Ipsum passages,
and more recently with desktop publishing
software like Aldus PageMaker including versions
of Lorem Ipsum.
- 6 years ago
- Zaid Bin Khalid
- 10,080 Views
-
8