How to Prefix all Textarea lines

Discussion in 'PHP' started by x.marty.x, Oct 12, 2014.

  1. #1
    I'm trying to make a simple generator that will take each line of a textarea, add a prefix and then output that data again in a textarea which can optionally be downloaded in a .txt file.

    Can anyone help?
    x.marty.x, Oct 12, 2014 IP
  2. pienisieni

    pienisieni Member

    Messages:
    27
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    48
    #2
    Do you really need to make your own?

    There are already tools for that online: http://textmechanic.com/Add-Prefix-Suffix-to-Text.html
    pienisieni, Oct 12, 2014 IP
  3. pienisieni

    pienisieni Member

    Messages:
    27
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    48
    #3
    Here you go:

    HTML:
    1. <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
    Code (Javascript):
    1. function AddTags(name, prefix, suffix) {
    2.     try {
    3.         var textArea = document.getElementById(name).value;
    4.         var i = 0;
    5.         var textArray = textArea.split("\n");
    6.         if (textArray === null) {
    7.             document.getElementById(name).value += prefix + suffix;
    8.         } else {
    9.             for (i = 0; i < textArray.length; i++) {
    10.                 textArray[i] = prefix + textArray[i] + suffix;
    11.             }
    12.             document.getElementById(name).value = textArray.join("\n");
    13.         }
    14.     } catch (err) {}
    15. }
    HTML:
    1. <textarea id="TextBox" cols="50" rows="20"></textarea>
    2. <button id="myButton" onclick="AddTags('TextBox', '[prefix]', '[/suffix]')">Add [p][/s]</button>
    If you don't need a suffix, just leave that blank.

    http://jsfiddle.net/chiappa/8v4kdwex/
    pienisieni, Oct 12, 2014 IP
  4. x.marty.x

    x.marty.x Active Member

    Messages:
    375
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    90
    #4
    Thanks so much, that's awesome.

    Any idea how it could be output in a .txt file for the visitor to download after hitting the button?
    x.marty.x, Oct 13, 2014 at 1:54 AM IP