Jump to content

[SOLVED] Typography


Recommended Posts

i love wandering.

 

it sounds like you'll want to use functions similar to nl2br(), which you can use when the data is outputted to the browser (NOT WHEN IT IS FIRST INSERTED INTO THE DATABASE).

 

after looking at the typography class page, it looks like there are a series of substitutions it will make to format the code correctly. most of this is regular expression substitution, but there may be pre-made classes out there for this.

Link to comment
https://forums.phpfreaks.com/topic/160823-solved-typography/#findComment-848781
Share on other sites

i love wandering.

 

it sounds like you'll want to use functions similar to nl2br(), which you can use when the data is outputted to the browser (NOT WHEN IT IS FIRST INSERTED INTO THE DATABASE).

 

after looking at the typography class page, it looks like there are a series of substitutions it will make to format the code correctly. most of this is regular expression substitution, but there may be pre-made classes out there for this.

 

ok i looked into nl2br() and stumbled across a page on the php.net site asd this guy wrote code that works with the <pre> tag and it works thanks for the help.

 

(I also changed the name of the function to typography)

 

//Typography Function

// do nl2br except when in a pre tag
function typography($string)
{
    // First, check for <pre> tag
    if(!strpos($string, "<pre>"))
    {
        return nl2br($string);
    }

    // If there is a <pre>, we have to split by line
    // and manually replace the linebreaks with <br />
    $strArr=explode("\n", $string);
    $output="";
    $preFound=false;

    // Loop over each line
    foreach($strArr as $line)
    {    // See if the line has a <pre>. If it does, set $preFound to true
        if(strpos($line, "<pre>"))
        {
            $preFound=true;
        }
        elseif(strpos($line, "</pre>"))
        {
            $preFound=false;
        }
       
        // If we are in a pre tag, just give a \n, else a <br />
        if($preFound)
        {
            $output .= $line . "\n";
        }
        else
        {
            $output .= $line . "<br />";
        }
    }

    return $output;
}

 

and the code to use it

 

<?php
echo typography($welcome);
?>

// OR

<?php
echo typography('Hello World!');
?>

Link to comment
https://forums.phpfreaks.com/topic/160823-solved-typography/#findComment-848799
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.