papillonstudios Posted June 3, 2009 Share Posted June 3, 2009 I used to use CodeIgniter PHP Framework and i was wandering how i would get my text to format from a database like the typography feature. Which basically is like all forum software the post people submit are displayed the same way they are typed. Link to comment https://forums.phpfreaks.com/topic/160823-solved-typography/ Share on other sites More sharing options...
akitchin Posted June 3, 2009 Share Posted June 3, 2009 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 More sharing options...
papillonstudios Posted June 3, 2009 Author Share Posted June 3, 2009 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 More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.