Add character to every line of text?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • ziycon
    Contributor
    • Sep 2008
    • 384

    Add character to every line of text?

    I have a function that takes a piece of text from the database and puts it into a textarea, I'm trying to put a symbol like '>' beside very line of text thats taken from the database and put into the textarea to show its taken from the database, can anyone help?
  • Dormilich
    Recognized Expert Expert
    • Aug 2008
    • 8694

    #2
    if you have defined the number of rows, then you can add before every (rows – 2) characters your indicator "> ".

    Comment

    • ziycon
      Contributor
      • Sep 2008
      • 384

      #3
      The problem is, its a textarea and when the first line is populated it goes to the next line so i don't know when it will go to the next line!?

      Comment

      • Atli
        Recognized Expert Expert
        • Nov 2006
        • 5062

        #4
        Hi.

        First of all, you can not put the symbol ">" into your HTML output without causing all sorts of problems. It is a reserved character.
        Use the HTML symbol ">" instead. (Without the quotes.)

        I'm not quite getting what you are trying to accomplish here.
        Do you want to put ">" before every line of text inside your textbox?
        If so, try adding the HTML symbol after each new-line character.
        (Replacing all occurences of "\n" with "\n>" should do that. Again, without the quotes)

        If not, please elaborate.
        Remember, we like code examples and error messages ;]

        Comment

        • ziycon
          Contributor
          • Sep 2008
          • 384

          #5
          I understand about the >. Ok I'll try and explain it better. I have a textarea on a page, when the page loads it get a a string, it can be a small or large string, the string is put into the textarea like below:
          Code:
          echo '<textarea name="body" cols="75" rows="10">'.getText().'</textarea>';
          Now, the text is wrapped automatically to fit into the textarea so ther is no new-line characters that I'm aware of.

          The textare is a message reply box so the text being taken from the database is the received message, i need to add '>' to show its the received text so when a user enters new text you can tell the difference.

          Hope this makes it a bit easier too understand.

          Comment

          • Atli
            Recognized Expert Expert
            • Nov 2006
            • 5062

            #6
            Ok, so you want the old message, the one from the database, to be sent along with the new text?

            Somewhat like:
            > This is some text from the database.
            > A text that should appear quoted.
            > Which means each line should start with
            > a ">" symbol.

            This is a new message.
            Something the current use would type
            into the textarea.
            If so, then you would just have to add the &gt; symbol on ever new-line.
            If there are no new-lines (if the text is one long line), you could split it into lines using the wordwrap function, and have the function add the &gt; symbol on every new line.

            Comment

            • ziycon
              Contributor
              • Sep 2008
              • 384

              #7
              Heres the function i was trying to use but with no luck:
              Everytime data is entered into the database it adds a <p></p> to every line, if you get me?
              Code:
              function formatReplyText($msg) {
                  $msg = str_replace("<p>&gt;&gt;","&gt;&gt;&gt;",$msg);
                  $msg = str_replace("<p>&gt;","&gt;&gt;",$msg);
                  $msg = str_replace("<p>","&gt;",$msg);
                  $msg = str_replace("</p>","<br /><p>",$msg);
                  
                  //$msg = wordwrap($msg, 12, "<br />\n");
                  //$msg = str_replace("&gt;&gt;","&gt;&gt;&gt;",$msg);
                  //$msg = str_replace("&gt;","&gt;&gt;",$msg);
                  //$msg = str_replace("<p>","&gt;",$msg);
                  
                  
                  
                  //$msg = str_replace("</p>","<br />\n",$msg);
                  
                  return $msg;
              }

              Comment

              • Atli
                Recognized Expert Expert
                • Nov 2006
                • 5062

                #8
                Is the return value of that supposed to go inside the <textarea>?

                If so, then you should not be using HTML tags in it.
                The value of a textarea should be formatted like normal text, not HTML.

                So, if your message were something like:
                Code:
                First line
                Second line
                Third line
                And you did something like this to it:
                [code=php]
                <?php
                function formatText($tex t) {
                $text = wordwrap($text, 50);
                $text = htmlentities($t ext, ENT_NOQUOTES, "UTF-8");
                $text = "&gt;" . str_replace("\n ", "\n&gt;", $text);

                return $text;
                }?>[/code]
                It would display the text in lines, each line no longer than 50 characters and each line prefixed with a > symbol.

                If each line is also encapsulated in <p>..</p> tags, you might want to simply remove them by using the str_replace function, passing each of the tags with an empty string.

                Originally posted by ziycon
                Everytime data is entered into the database it adds a <p></p> to every line, if you get me?
                This is a very very bad thing to do. You should never format your data before putting it into the database.
                What if you ever decide to change the format? You would have to update you entire database to fit your new format, which puts it at risk of corruption and all sorts of other problems.

                The database should always store the original text. Whatever alterations you need to make before it is displayed on your front-end application should be done on the way out.

                Comment

                • ziycon
                  Contributor
                  • Sep 2008
                  • 384

                  #9
                  Its showing up like so when i got to reply:
                  >

                  Test message.
                  I'm using TinyMCE as the editor in the textarea and TinyMCE adds <p></p> on every line.

                  Comment

                  • Atli
                    Recognized Expert Expert
                    • Nov 2006
                    • 5062

                    #10
                    If your using the function I just posted, that would mean you are passing it an empty string. (Just made that mistake myself just now... misspelled the variable :P)

                    This:
                    [code=php]<?php
                    function formatText($tex t) {
                    $text = wordwrap($text, 70);
                    $text = htmlentities($t ext, ENT_NOQUOTES, "UTF-8");
                    $text = "&gt; " . str_replace("\n ", "\n&gt; ", $text);

                    return $text;
                    }

                    $message = <<<TEXT
                    Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed elementum. Pellentesque mattis. Vestibulum nisl tortor, blandit ut, tincidunt at, fermentum at, massa. Praesent dictum semper justo. Fusce et massa. Sed nisi tellus, venenatis vel, condimentum nec, iaculis sollicitudin, turpis. Ut sit amet urna eget eros ullamcorper ullamcorper. Integer volutpat.
                    TEXT;
                    ?>
                    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
                    <html>
                    <head>
                    <title>Test</title>
                    </head>
                    <body>
                    <textarea cols="72" rows="10"><?php echo formatText($mes sage); ?></textarea>
                    </body>
                    </html>[/code]
                    Gives:
                    [code=html]<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
                    <html>
                    <head>
                    <title>Test</title>
                    </head>
                    <body>
                    <textarea cols="72" rows="10">&gt; Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed
                    &gt; elementum. Pellentesque mattis. Vestibulum nisl tortor, blandit ut,
                    &gt; tincidunt at, fermentum at, massa. Praesent dictum semper justo. Fusce
                    &gt; et massa. Sed nisi tellus, venenatis vel, condimentum nec, iaculis
                    &gt; sollicitudin, turpis. Ut sit amet urna eget eros ullamcorper
                    &gt; ullamcorper. Integer volutpat. </textarea>
                    </body>
                    </html>[/code]

                    Comment

                    • ziycon
                      Contributor
                      • Sep 2008
                      • 384

                      #11
                      Got it working fine without TinyMCE being used. One last thing about it, how would i go about adding a blank line under the reply text to seperate it from the new message?

                      Comment

                      • Dormilich
                        Recognized Expert Expert
                        • Aug 2008
                        • 8694

                        #12
                        adding two consecutive newline characters (e.g. "\n\n")

                        Comment

                        • ziycon
                          Contributor
                          • Sep 2008
                          • 384

                          #13
                          Originally posted by Dormilich
                          adding two consecutive newline characters (e.g. "\n\n")
                          Tried this so many ways but its just putting an empty line under every line instead of just one empty line after all the text? I really appreciate all your help.

                          Comment

                          • Dormilich
                            Recognized Expert Expert
                            • Aug 2008
                            • 8694

                            #14
                            Originally posted by ziycon
                            but its just putting an empty line under every line instead of just one empty line after all the text?
                            where do you put the double newline? if I assume you use Atli's function, putting it right after the conversion has finished should do it.
                            Code:
                            function formatText($text) {
                            // code ...
                                return $text . "\n\n";
                            }

                            Comment

                            • ziycon
                              Contributor
                              • Sep 2008
                              • 384

                              #15
                              Got it working perfectly, thanks ever so much!

                              Comment

                              Working...