The forum of the forums
Welcome to the Official Support Forum of Forumotion!

To take full advantage of everything offered by our forum, please log in if you are already a member, or join our community if you've not yet.



Create a free forum like this one.

closing vbulletin tags in a string

View previous topic View next topic Go down

closing vbulletin tags in a string

Post by _Twisted_Mods_ on November 9th 2016, 6:39 pm

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]'
avatar
_Twisted_Mods_
Helper
Helper

Male Posts : 2032
Reputation : 302
Language : English
Location : Ms

http://liquidcode.forumotion.com

Back to top Go down

Re: closing vbulletin tags in a string

Post by Ch@lo Valdez on November 10th 2016, 1:37 am

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
avatar
Ch@lo Valdez
Forumember

Male Posts : 124
Reputation : 40
Language : spanish

Back to top Go down

Re: closing vbulletin tags in a string

Post by _Twisted_Mods_ on November 10th 2016, 2:27 pm

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
avatar
_Twisted_Mods_
Helper
Helper

Male Posts : 2032
Reputation : 302
Language : English
Location : Ms

http://liquidcode.forumotion.com

Back to top Go down

Re: closing vbulletin tags in a string

Post by Ch@lo Valdez on November 10th 2016, 3:19 pm

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 November 10th 2016, 3:42 pm; edited 1 time in total
avatar
Ch@lo Valdez
Forumember

Male Posts : 124
Reputation : 40
Language : spanish

Back to top Go down

Re: closing vbulletin tags in a string

Post by _Twisted_Mods_ on November 10th 2016, 3:29 pm

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

Code:

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

Male Posts : 2032
Reputation : 302
Language : English
Location : Ms

http://liquidcode.forumotion.com

Back to top Go down

Re: closing vbulletin tags in a string

Post by Ch@lo Valdez on November 10th 2016, 3:40 pm

@_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 ?
avatar
Ch@lo Valdez
Forumember

Male Posts : 124
Reputation : 40
Language : spanish

Back to top Go down

Re: closing vbulletin tags in a string

Post by _Twisted_Mods_ on November 10th 2016, 3:45 pm

sorry. it is an error because the color tag did not close before the next one started
avatar
_Twisted_Mods_
Helper
Helper

Male Posts : 2032
Reputation : 302
Language : English
Location : Ms

http://liquidcode.forumotion.com

Back to top Go down

Re: closing vbulletin tags in a string

Post by Ch@lo Valdez on November 10th 2016, 4:00 pm

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]'
avatar
Ch@lo Valdez
Forumember

Male Posts : 124
Reputation : 40
Language : spanish

Back to top Go down

Re: closing vbulletin tags in a string

Post by _Twisted_Mods_ on November 10th 2016, 4:13 pm

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

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



basically i want it to close all color tags and not allow them to be inside each other
avatar
_Twisted_Mods_
Helper
Helper

Male Posts : 2032
Reputation : 302
Language : English
Location : Ms

http://liquidcode.forumotion.com

Back to top Go down

Re: closing vbulletin tags in a string

Post by Ch@lo Valdez on November 10th 2016, 4:27 pm

mmmm let me think

send me the url of your blog or forum please
avatar
Ch@lo Valdez
Forumember

Male Posts : 124
Reputation : 40
Language : spanish

Back to top Go down

Re: closing vbulletin tags in a string

Post by _Twisted_Mods_ on November 10th 2016, 4:39 pm

sent u a pm
avatar
_Twisted_Mods_
Helper
Helper

Male Posts : 2032
Reputation : 302
Language : English
Location : Ms

http://liquidcode.forumotion.com

Back to top Go down

Re: closing vbulletin tags in a string

Post by SLGray on November 10th 2016, 9:30 pm

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


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

avatar
SLGray
Administrator
Administrator

Male Posts : 37985
Reputation : 2561
Language : English
Location : United States

http://ztwds.forumotion.com/

Back to top Go down

Re: closing vbulletin tags in a string

Post by _Twisted_Mods_ on November 11th 2016, 12:19 am

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);
         }
      };
avatar
_Twisted_Mods_
Helper
Helper

Male Posts : 2032
Reputation : 302
Language : English
Location : Ms

http://liquidcode.forumotion.com

Back to top Go down

Re: closing vbulletin tags in a string

Post by LGforum on November 22nd 2016, 4:22 am

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
avatar
LGforum
Hyperactive

Male Posts : 2262
Reputation : 260
Language : English
Location : UK

http://www.avacweb.com/

Back to top Go down

View previous topic View next topic Back to top


 
Permissions in this forum:
You cannot reply to topics in this forum