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!

Link to comment
Share on other sites

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
Share on other sites

 

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
Share on other sites

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
Share on other sites

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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.