Multiline String Output Format Function
Posted: Wed Sep 30, 2009 3:26 pm
I hope this is the right spot for this kind of thread...
Anyway, I made a script where one function can output multiple lines of strings with formatting options including the HTML break tag.
It's main use is for writing HTML. I wanted to make this because I wanted more control with my tab space and new line counts when formatting my HTML code on-the-fly.
It's a little slow, but it works. Here's the basic syntax:
The function will try to find int values in its parameters and attempt to store them as parameters for the string. Except for $keeptab, The integer values are multipliers for each type of string format character(s). If there are no int values, then it is returned with at least one newline at the end of the string and, if the $tabspace has been stored, will add the tab spaces in front of the string.
- Newlines go at the end of the string
- HTML Break tags go before the newlines
- Tab spaces go in front of the string
- $keeptab is a switch for $tabspace multiplier in which it will save the last int value for the next string
Multiple strings and integer values can be used. The function will look for the string's corresponding int values. The int values above must be in that order for the function to work as intended.
Here's a more complex syntax:
Here's the code:
Anyway, I made a script where one function can output multiple lines of strings with formatting options including the HTML break tag.
It's main use is for writing HTML. I wanted to make this because I wanted more control with my tab space and new line counts when formatting my HTML code on-the-fly.
It's a little slow, but it works. Here's the basic syntax:
Code: Select all
mnl( string $string, int $newline, int $htmlbreak, int $tabspace, int $keeptab )- Newlines go at the end of the string
- HTML Break tags go before the newlines
- Tab spaces go in front of the string
- $keeptab is a switch for $tabspace multiplier in which it will save the last int value for the next string
Multiple strings and integer values can be used. The function will look for the string's corresponding int values. The int values above must be in that order for the function to work as intended.
Here's a more complex syntax:
Code: Select all
mnl( $string1, // One newline
$string2, // One newline
$string3, 2, // Two newlines
$string4, // One newline
$string5, 4, 4, // Four newline and Four Break Tags
$string6, 1, 0, 1, 1, // One newline, 1 tab space and save tab count
$string7, // One newline, 1 tab space
$string8, // One newline, 1 tab space
$string9 1, 0, 0, 0, // One newline, 0 tab spaces and remove tab count
)Code: Select all
<?php
echo mnl( 'This is line 1 with 1 tab and lose tab', 1, 0, 1,
'This is line 2',
'This is line 3 with 2 newlines, 2 breaks, 1 tab and keep tab', 2, 2, 1, 1,
'This is line 4',
'This is line 5 with 4 newlines, 4 breaks, no tabs and lose tab', 4, 4, 0, 0,
'This is line 6' );
function mnl()
{
$args = func_get_args();
$argsnum = func_num_args();
$argskeys = $argsnum - 1;
for( $x = 0; $x <= $argskeys; $x++ )
{
$intnum = 0;
if( ($intkey=$x+1) <= $argsnum && is_int($args[$intkey]) )
{
while( ($intnum < 4 && $intkey <= $argsnum) && is_int($args[$intkey]) )
{
$intstr[++$intnum] = $args[($y=$intkey++)];
}
if( isset($intstr[4]) && ($intstr[4] >= 0 && $intstr[4] <= 1) ){
if( $intstr[4] == 1 )
$tabnum = $intstr[3];
else
unset( $tabnum );
}
}
else
$intstr[1] = 1;
$strout[count($strout)] = str_repeat( "\t", ($tabnum ? $tabnum : $intstr[3]) )
.$args[$x]
.str_repeat( "<br />", $intstr[2] )
.str_repeat( PHP_EOL, $intstr[1] );
if( isset($y) ){
$x = $y; unset( $y );
}
unset( $intstr );
}
return implode( $strout );
}
?>