Jump to content

I want to insert a few \t\t after using nl2br


cgm225

Recommended Posts

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!

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

 

 

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

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 />

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,

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.