Page 1 of 1

Multiline String Output Format Function

Posted: Wed Sep 30, 2009 3:26 pm
by KyleReed
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:

Code: Select all

mnl( string $string, int $newline, int $htmlbreak, int $tabspace, int $keeptab )
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:

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
)
Here's the code:

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 );
}
?>

Re: Multiline String Output Format Function

Posted: Wed Sep 30, 2009 5:40 pm
by Christopher
Ok ... I'll say it ... that looks a little crazy! :roll:

I think you would be better off using templates. I guess I could see if you had an array of strings that you wanted to format, maybe in a pattern or something, but parameters!?! It looks like you have produced an excellent solution in the wrong direction. ;)

Re: Multiline String Output Format Function

Posted: Wed Sep 30, 2009 8:04 pm
by KyleReed
Yeah. I know it's crazy.

Here a simple function that does newlines and HTML breaks that from what I can see, runs pretty good. I was just trying to make a more complex version:

Code: Select all

function nlstr( $str, $nlnum=1, $brnum=0 )
{
    return $str.( $brnum ? str_repeat('<br />', $brnum) : '' ).str_repeat( PHP_EOL, $nlnum );
}

Re: Multiline String Output Format Function

Posted: Thu Oct 01, 2009 3:19 am
by josh
Got before and after HTM / XHTML? I'm not following

Re: Multiline String Output Format Function

Posted: Thu Oct 01, 2009 8:59 am
by KyleReed
What?

Could you rephrase the question? I don't understand.