The forum of the forums

Would you like to react to this message? Create an account in a few clicks or log in to continue.
The forum of the forums
4 posters

    closing vbulletin tags in a string

    _Twisted_Mods_
    _Twisted_Mods_
    Helper
    Helper


    Male Posts : 2083
    Reputation : 336
    Language : English
    Location : Ms

    closing vbulletin tags in a string Empty closing vbulletin tags in a string

    Post by _Twisted_Mods_ Wed 9 Nov 2016 - 18:39

    ok so what im wanting it to prevent errors in a string example

    Code:

    str = '[color=red]just some random[color=green]text[/color][/color]'

    the sting wouldnt be right because the color tags are inside each other so what im wanting to do is make a function that will check the string to make sure all color tags are closed correct and return a result like this

    Code:

    str = '[color=red]just some random[/color][color=green]text[/color]'
    Ch@lo Valdez
    Ch@lo Valdez
    Forumember


    Male Posts : 138
    Reputation : 50
    Language : spanish

    closing vbulletin tags in a string Empty Re: closing vbulletin tags in a string

    Post by Ch@lo Valdez Thu 10 Nov 2016 - 1:37

    This is probably useful for you

    Code:

    function str_replace(c){
        var a = /\[color=(\w+)\](.*?)\[color=(.*?)\](\w+)\[\/color\]\[\/color\]/,
            b = '[color=red]$2[/color][color=green]$4[/color]';
     return c.replace(a, b)
    };

    use this way:

    Code:
    text = document.getElementsByTagName('textarea')[0]; //or jQuery $('textarea').val()
    text.value = str_replace(text.value);

    maybe with more details can do something better
    _Twisted_Mods_
    _Twisted_Mods_
    Helper
    Helper


    Male Posts : 2083
    Reputation : 336
    Language : English
    Location : Ms

    closing vbulletin tags in a string Empty Re: closing vbulletin tags in a string

    Post by _Twisted_Mods_ Thu 10 Nov 2016 - 14:27

    ok well im usen a script to color highlighted text in a textbox but i want to make sure each vbulletin color tag is closed correct and it dont put color tags inside each other and send it incorrect

    example if i put somthing like this in the text box and submit it

    Code:

    [color=#8333ff]just testing [color=#0000ff]some stuff[/color][/color]

    the html will turn out like this

    Code:

    <div><span style="color:#8333ff;">just[color=#0000ff] testing</span> some stuff [/color]</div>


    and its because one color code wasnt ended before the next one started its inside each other if that makes any sense
    Ch@lo Valdez
    Ch@lo Valdez
    Forumember


    Male Posts : 138
    Reputation : 50
    Language : spanish

    closing vbulletin tags in a string Empty Re: closing vbulletin tags in a string

    Post by Ch@lo Valdez Thu 10 Nov 2016 - 15:19

    this regexp work for that you want
    Code:
    a = /\[color=(\w+)\](.*?)\[color=(.*?)\](\w+)\[\/color\]\[\/color\]/;
    b = '[color=$1]$2[/color][color=$3]$4[/color]';

    you can make a js prevent this situation before submit for example:

    Code:
    your_submit_button.onclick = function(){
    textarea = your textarea here;
    a = /\[color=(.*?)\](.*?)\[color=(.*?)\](.*?)\[\/color\]\[\/color\]/;
    b = '[color=$1]$2[/color][color=$3]$4[/color]';
    c= textarea.value.match(a);
    if (c){
    textarea.value = textarea.value.replace(a, b);
    return false
    }else{
    //go submit
    }
    };


    Last edited by Ch@lo Valdez on Thu 10 Nov 2016 - 15:42; edited 1 time in total
    _Twisted_Mods_
    _Twisted_Mods_
    Helper
    Helper


    Male Posts : 2083
    Reputation : 336
    Language : English
    Location : Ms

    closing vbulletin tags in a string Empty Re: closing vbulletin tags in a string

    Post by _Twisted_Mods_ Thu 10 Nov 2016 - 15:29

    what it the end color tag isnt next to each other like this

    Code:

    [color=#8333ff]just testing [color=#0000ff]some[/color] stuff[/color]
    Ch@lo Valdez
    Ch@lo Valdez
    Forumember


    Male Posts : 138
    Reputation : 50
    Language : spanish

    closing vbulletin tags in a string Empty Re: closing vbulletin tags in a string

    Post by Ch@lo Valdez Thu 10 Nov 2016 - 15:40

    _Twisted_Mods_ wrote:what it the end color tag isnt next to each other like this

    Code:

    [color=#8333ff]just testing [color=#0000ff]some[/color] stuff[/color]

    this is another error string to fix or the result you want ?
    _Twisted_Mods_
    _Twisted_Mods_
    Helper
    Helper


    Male Posts : 2083
    Reputation : 336
    Language : English
    Location : Ms

    closing vbulletin tags in a string Empty Re: closing vbulletin tags in a string

    Post by _Twisted_Mods_ Thu 10 Nov 2016 - 15:45

    sorry. it is an error because the color tag did not close before the next one started
    Ch@lo Valdez
    Ch@lo Valdez
    Forumember


    Male Posts : 138
    Reputation : 50
    Language : spanish

    closing vbulletin tags in a string Empty Re: closing vbulletin tags in a string

    Post by Ch@lo Valdez Thu 10 Nov 2016 - 16:00

    how you want the final fix string? in this case we have 5 elements
    Code:
    [color=#8333ff]just testing [color=#0000ff]some[/color] stuff[/color]

    1$ is '#8333ff'
    2$ is 'just testing'
    3$ is '#0000ff'
    4$ is 'some'
    5$ is 'stuff'

    in replace
    Code:
    '[color=$1]$2[/color][color=$3]$4[/color]'

    Where should element 5 go?

    Code:
    '[color=$1]$2[/color][color=$3]$4 $5[/color]'
    _Twisted_Mods_
    _Twisted_Mods_
    Helper
    Helper


    Male Posts : 2083
    Reputation : 336
    Language : English
    Location : Ms

    closing vbulletin tags in a string Empty Re: closing vbulletin tags in a string

    Post by _Twisted_Mods_ Thu 10 Nov 2016 - 16:13

    ok lest say this is the input

    Code:

    [color=#8333ff]just [color=#ff0000]testing [color=#0000ff]some[/color][/color] stuff[/color]

    this is the result i would get
    closing vbulletin tags in a string B166014f28554f778da866de4868df5e
    the correct way should be

    Code:

    [color=#8333ff] just[/color] [color=#ff0000]testing [/color][color=#0000ff]some[/color][color=#8333ff]stuff[/color]

    and the result would look like this

    closing vbulletin tags in a string 80061e1fff774c52b6a1f9df2cd04102

    basically i want it to close all color tags and not allow them to be inside each other
    Ch@lo Valdez
    Ch@lo Valdez
    Forumember


    Male Posts : 138
    Reputation : 50
    Language : spanish

    closing vbulletin tags in a string Empty Re: closing vbulletin tags in a string

    Post by Ch@lo Valdez Thu 10 Nov 2016 - 16:27

    mmmm let me think

    send me the url of your blog or forum please
    _Twisted_Mods_
    _Twisted_Mods_
    Helper
    Helper


    Male Posts : 2083
    Reputation : 336
    Language : English
    Location : Ms

    closing vbulletin tags in a string Empty Re: closing vbulletin tags in a string

    Post by _Twisted_Mods_ Thu 10 Nov 2016 - 16:39

    sent u a pm
    SLGray
    SLGray
    Administrator
    Administrator


    Male Posts : 51499
    Reputation : 3523
    Language : English
    Location : United States

    closing vbulletin tags in a string Empty Re: closing vbulletin tags in a string

    Post by SLGray Thu 10 Nov 2016 - 21:30

    I believe you meant to say BBCode, not vBulletin tag codes.



    closing vbulletin tags in a string Slgray10

    When your topic has been solved, ensure you mark the topic solved.
    Never post your email in public.
    _Twisted_Mods_
    _Twisted_Mods_
    Helper
    Helper


    Male Posts : 2083
    Reputation : 336
    Language : English
    Location : Ms

    closing vbulletin tags in a string Empty Re: closing vbulletin tags in a string

    Post by _Twisted_Mods_ Fri 11 Nov 2016 - 0:19

    correct SL idk what i was on when i wrote that but ty...


    ok so i found this idk if it will help any

    Code:

     * Fixes any invalid nesting.
           *
           * If it is a block level element inside 1 or more inline elements
           * then those inline elements will be split at the point where the
           * block level is and the block level element placed between the split
           * parts. i.e.
           *    [inline]textA[blocklevel]textB[/blocklevel]textC[/inline]
           * Will become:
           *    [inline]textA[/inline][blocklevel]textB[/blocklevel][inline]textC[/inline]
           *
           * @param {Array} children
           * @param {Array} [parents] Null if there is no parents
           * @param {Array} [insideInline] Boolean, if insdie an inline element
           * @param {Array} [rootArr] Root array if there is one
           * @return {Array}
           * @private
           */
          fixNesting = function(children, parents, insideInline, rootArr) {
             var   token, i, parent, parentIndex, parentParentChildren, right,
                isInline = function(token) {
                   var bbcode = base.bbcodes[token.name];

                   return !bbcode || bbcode.isInline !== false;
                };

             parents = parents || [];
             rootArr = rootArr || children;

             // this must check length each time as the length
             // can change as tokens are moved around to fix the nesting.
             for(i=0; i<children.length; i++)
             {
                if(!(token = children[i]) || token.type !== tokenType.open)
                   continue;

                if(!isInline(token) && insideInline)
                {
                   // if this is a blocklevel element inside an inline one then split
                   // the parent at the block level element
                   parent              = last(parents);
                   right                = parent.splitAt(token);
                   parentParentChildren = parents.length > 1 ? parents[parents.length - 2].children : rootArr;

                   if((parentIndex = $.inArray(parent, parentParentChildren)) > -1)
                   {
                      // remove the block level token from the right side of the split
                      // inlnie element
                      right.children.splice($.inArray(token, right.children), 1);

                      // insert the block level token and the right side after the left
                      // side of the inline token
                      parentParentChildren.splice(parentIndex+1, 0, token, right);

                      // return to parents loop as the children have now increased
                      return;
                   }

                }

                parents.push(token);
                fixNesting(token.children, parents, insideInline || isInline(token), rootArr);
                parents.pop(token);
             }
          };
    LGforum
    LGforum
    Hyperactive


    Male Posts : 2265
    Reputation : 264
    Language : English
    Location : UK

    closing vbulletin tags in a string Empty Re: closing vbulletin tags in a string

    Post by LGforum Tue 22 Nov 2016 - 4:22

    I'm curious as to why you want to fix this? Color tags within color tags isn't a bad thing or even incorrect, if anything its an even more efficient use of tags and I'm pretty sure it will be parsed correctly by the forum and will be displayed correctly in posts, won't it?

    Lets try:
    just some random text

    See, it works fine Smile

      Current date/time is Mon 23 Sep 2024 - 9:31