How to create a string that has new lines and tab characters in it?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Robizzle

    #1

    How to create a string that has new lines and tab characters in it?

    I'm writting a script that produces html source code from form input.
    My problem is that I want to format my html source with new lines and
    tabs.

    So, I have a string that holds all the contents of the source and my
    php script displays it by saying

    echo '<form><textare a>'.$sourcestri ng.'</textarea></html>'; All of
    that is working currectly except that I don't know how to get the tabs
    and new lines in the string.

    Here is what I have tried while assinging into the string:

    /* This one works but I know there is another way, I hate leaving a php
    string open and spanning multiple lines like this */
    $sourcestring = '<div class="news" align=left>
    <div class="news_tit lebar">
    <table width=100% border=0>
    <tr><td><u>Subj ect Line</u></td>
    <td align="right">< u>Thu, October the 20th 2005 at 7:23pm EST</td>
    </tr>
    </table>
    </div>
    <center><hr width=95% /></center>
    <div class="news_bod y">Body of the news post here
    <br>
    </div>
    </div>';

    Notice that the '....' remains open for multiple lines of code and that
    entire block there is a single assignment expression -- ewww, nasty.

    /*This next way is how I tried to fix it, however, it didn't work. The
    \t and \n were output to the screen by the echo command. Meaning my
    page actually had the backslash and the letters t and n riddled
    throughout */
    $string = '<div class="news" align=left>\n\t <div
    class="news_tit lebar">\n\t\t<t able width=100%
    border=0>\n\t\t \t<tr><td><u>'. 'Subject Line'.'</u></td>\n\t\t\t\t<t d
    align="right">< u>'.'Thu, October the 20th 2005 at 7:23pm
    EST'.'</td>\n\t\t\t</tr>\n\t\t</table>\n\t</div>\n\t<center ><hr
    width=95% /></center>\n\t<div class="news_bod y">'.'Body of the news
    post here'.'\n\t<br> \n\t</div>\n</div>';

    I've looked everwhere and all I can figure out is that the escape
    characters for strings in php should be \n and \t however these aren't
    working for me. Like I said earlier, if I was to say echo '\n\t\n'; I
    would literally end up seeing "\n\t\n" printed to the screen instead of
    a new line, then a line with only a tab, then a new line.

  • Juliette

    #2
    Re: How to create a string that has new lines and tab charactersin it?

    Robizzle wrote:[color=blue]
    > I'm writting a script that produces html source code from form input.
    > My problem is that I want to format my html source with new lines and
    > tabs.
    >
    > So, I have a string that holds all the contents of the source and my
    > php script displays it by saying
    >
    > echo '<form><textare a>'.$sourcestri ng.'</textarea></html>'; All of
    > that is working currectly except that I don't know how to get the tabs
    > and new lines in the string.
    >
    > Here is what I have tried while assinging into the string:
    >
    > /* This one works but I know there is another way, I hate leaving a php
    > string open and spanning multiple lines like this */
    > $sourcestring = '<div class="news" align=left>
    > <div class="news_tit lebar">
    > <table width=100% border=0>
    > <tr><td><u>Subj ect Line</u></td>
    > <td align="right">< u>Thu, October the 20th 2005 at 7:23pm EST</td>
    > </tr>
    > </table>
    > </div>
    > <center><hr width=95% /></center>
    > <div class="news_bod y">Body of the news post here
    > <br>
    > </div>
    > </div>';
    >
    > Notice that the '....' remains open for multiple lines of code and that
    > entire block there is a single assignment expression -- ewww, nasty.
    >
    > /*This next way is how I tried to fix it, however, it didn't work. The
    > \t and \n were output to the screen by the echo command. Meaning my
    > page actually had the backslash and the letters t and n riddled
    > throughout */
    > $string = '<div class="news" align=left>\n\t <div
    > class="news_tit lebar">\n\t\t<t able width=100%
    > border=0>\n\t\t \t<tr><td><u>'. 'Subject Line'.'</u></td>\n\t\t\t\t<t d
    > align="right">< u>'.'Thu, October the 20th 2005 at 7:23pm
    > EST'.'</td>\n\t\t\t</tr>\n\t\t</table>\n\t</div>\n\t<center ><hr
    > width=95% /></center>\n\t<div class="news_bod y">'.'Body of the news
    > post here'.'\n\t<br> \n\t</div>\n</div>';
    >
    > I've looked everwhere and all I can figure out is that the escape
    > characters for strings in php should be \n and \t however these aren't
    > working for me. Like I said earlier, if I was to say echo '\n\t\n'; I
    > would literally end up seeing "\n\t\n" printed to the screen instead of
    > a new line, then a line with only a tab, then a new line.
    >[/color]


    The escape characters only work if you quote the string with double
    quotes (") rather than single quotes (').

    Have a read through this page for more information:
    PHP is a popular general-purpose scripting language that powers everything from your blog to the most popular websites in the world.

    Comment

    Working...