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 Active Member

    Messages:
    30
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    53
    #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 Active Member

    Messages:
    30
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    53
    #3
    Here you go:

    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
    HTML:
    function AddTags(name, prefix, suffix) {
        try {
            var textArea = document.getElementById(name).value;
            var i = 0;
            var textArray = textArea.split("\n");
            if (textArray === null) {
                document.getElementById(name).value += prefix + suffix;
            } else {
                for (i = 0; i < textArray.length; i++) {
                    textArray[i] = prefix + textArray[i] + suffix;
                }
                document.getElementById(name).value = textArray.join("\n");
            }
        } catch (err) {}
    }
    Code (JavaScript):
    <textarea id="TextBox" cols="50" rows="20"></textarea>
    <button id="myButton" onclick="AddTags('TextBox', '[prefix]', '[/suffix]')">Add [p][/s]</button>
    HTML:
    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 Well-Known Member

    Messages:
    398
    Likes Received:
    13
    Best Answers:
    0
    Trophy Points:
    140
    #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 IP