cgm225 Posted March 21, 2008 Share Posted March 21, 2008 I am currently grabbing blog entries form a mysql database, and then using nl2br() to add <br /> tags for line breaks. However, I also want to format the outputted source code such that each new line (now with a <br />) has some \t\t (tabs) before it... Any ideas? Thank you in advance! Link to comment https://forums.phpfreaks.com/topic/97280-i-want-to-insert-a-few-tt-after-using-nl2br/ Share on other sites More sharing options...
uniflare Posted March 21, 2008 Share Posted March 21, 2008 are you saying you want to indent your code? dynamically? or just indent all code given to a specific function (like part of a script or some random text) Link to comment https://forums.phpfreaks.com/topic/97280-i-want-to-insert-a-few-tt-after-using-nl2br/#findComment-497818 Share on other sites More sharing options...
cgm225 Posted March 21, 2008 Author Share Posted March 21, 2008 I want to indent each line of the echoed/outputted code returned by the nl2br() function with two tabs. Link to comment https://forums.phpfreaks.com/topic/97280-i-want-to-insert-a-few-tt-after-using-nl2br/#findComment-497823 Share on other sites More sharing options...
tendrousbeastie Posted March 21, 2008 Share Posted March 21, 2008 Hi, I guess you've notice that sticking the BR tags in the forum post has just caused your message to break lines where you've put them. Not to worry. To add some other control characters along with the BRs when using the nl2BR function your can use the str_replace function. Immediately after running you nl2br run... str_replace('<br />', '\\t\\t<br />', $the_string_in_question); This would add the \t\t before each each html line break. Is this what you're after Link to comment https://forums.phpfreaks.com/topic/97280-i-want-to-insert-a-few-tt-after-using-nl2br/#findComment-497827 Share on other sites More sharing options...
Barand Posted March 21, 2008 Share Posted March 21, 2008 As the browser ignores tabs, along with other whitespace characters, you will need to use CSS http://www.w3schools.com/css/tryit.asp?filename=trycss_text-indent Link to comment https://forums.phpfreaks.com/topic/97280-i-want-to-insert-a-few-tt-after-using-nl2br/#findComment-497829 Share on other sites More sharing options...
cgm225 Posted March 21, 2008 Author Share Posted March 21, 2008 @tendrousbeastie-- That is what I am after, but your example simply echoes "\t" and does not insert a tab.. @Barand - I am only looking to tab the source code (that which you see when you use your browser to "View source") Link to comment https://forums.phpfreaks.com/topic/97280-i-want-to-insert-a-few-tt-after-using-nl2br/#findComment-497848 Share on other sites More sharing options...
Northern Flame Posted March 21, 2008 Share Posted March 21, 2008 do this: str_replace('<br />', "\t\t<br />", $the_string_in_question); Link to comment https://forums.phpfreaks.com/topic/97280-i-want-to-insert-a-few-tt-after-using-nl2br/#findComment-497879 Share on other sites More sharing options...
cgm225 Posted March 21, 2008 Author Share Posted March 21, 2008 What am I replacing with that? Link to comment https://forums.phpfreaks.com/topic/97280-i-want-to-insert-a-few-tt-after-using-nl2br/#findComment-497880 Share on other sites More sharing options...
Northern Flame Posted March 21, 2008 Share Posted March 21, 2008 Immediately after running you nl2br run... str_replace('<br />', '\\t\\t<br />', $the_string_in_question); im just editing his code, str_replace('<br />', "\t\t<br />", $the_string_in_question); i just replace the single quotes with double quotes and took out a back slash before each t, that should do the trick Link to comment https://forums.phpfreaks.com/topic/97280-i-want-to-insert-a-few-tt-after-using-nl2br/#findComment-497884 Share on other sites More sharing options...
cgm225 Posted March 21, 2008 Author Share Posted March 21, 2008 nl2br() seems to insert a <br /> tag and carriage return into the outputted code.. is there anyway to have it simply insert the <br /> tag and not the carriage return? Link to comment https://forums.phpfreaks.com/topic/97280-i-want-to-insert-a-few-tt-after-using-nl2br/#findComment-497886 Share on other sites More sharing options...
cgm225 Posted March 21, 2008 Author Share Posted March 21, 2008 Example of what I want: Database entry: blah blah blah blah blah blah I run the nl2br function, and do a string replace, and I want the outputted source code to appear as such with tabs and breaks:: blah blah blah <br /> <br /> blah blah blah <br /> Link to comment https://forums.phpfreaks.com/topic/97280-i-want-to-insert-a-few-tt-after-using-nl2br/#findComment-497893 Share on other sites More sharing options...
uniflare Posted March 22, 2008 Share Posted March 22, 2008 lol ok try this: $tabbed_code = preg_replace("/([\n\r]+)/i","<br />\n\t\t",$string); this will replace every Newline or Carriage Return character into a "<BR />\n\t\t", it should also replace any \r\n (windows) type newlines. tested with this script: <?php $pattern = "/([\n\r]+)/i"; $replacement = "<br />\n\t\t"; $code = " This code should all be tabbed: <table width=''> <tr> <td></td> </tr> </table>"; $result = preg_replace($pattern,$replacement,$code); print_r($result); ?> ---- bearing in mind it will not add tabs for the first line simply because there is no line feed character, simply add "\t\t" to the beginning of the resulting string, ;like so: $result = "\t\t".$result; hope this helps, Link to comment https://forums.phpfreaks.com/topic/97280-i-want-to-insert-a-few-tt-after-using-nl2br/#findComment-498024 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.