Jump to content

Indent problems with styling of HTML source code while using nl2br function...


Recommended Posts

I have a string of text in a variable, let's say:

 

$text = "<a href='example.com'>Example.com</a>\n<a href='test.com'>Test.com</a>"; 

 

...and I echo the string with nl2br...

 

echo nl2br($text);

 

...which returns in my html source code as:

 

<a href='example.com'>Example.com</a><br />
<a href='test.com'>Test.com</a>

 

This is fine, but I am having the following problem when I use it as follows...

<html>
...snip...

     <a href='somelink.com'>somelink.com</a><br />
     <?php echo nl2br($text); ?>
     <a href='somelink.com'>Example.com</a><br />

...snip...
</html>

 

...because the nice indentation of the html source code is not preserved, and looks something like...

<html>
...snip...

     <a href='somelink.com'>somelink.com</a><br />
     <a href='example.com'>Example.com</a><br />
<a href='test.com'>Test.com</a>
     <a href='somelink.com'>Example.com</a><br />

...snip...
</html>

 

Here is my question... how do I get every new line created with the nl2br function to also get an indentation in the html source code?

 

Thanks in advance!

  • 3 weeks later...
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.