Jump to content

Recommended Posts

Hello all, I'm trying to split a html element into arguments

example:

<img src="Style/Images/Sprites/Civilian/base-male.png" height="75px" alt="Civilian Img" />

into:

Array("tagName" : "img" , "src" : "Style/Imag...png", "height" : "75px", "alt" : "Civilian Img");

but I haven't got a clue how to do it.

 

any tips/tricks on how to pull this off?

Link to comment
https://forums.phpfreaks.com/topic/158581-solved-splitting-an-html-element/
Share on other sites

I'd probably split the HTML by white space, then run a few regular expressions on each part to determine if it's the opening tag, an attribute, the end, etc. Probs need to watch out for things like "checked" - 1 word arguements. Best place to start is to work out what's distinct for each part, how you can recognize them. For example the start tag will have a '<' on the left, attributes will have the equals: 'something=something'.. etc. etc.

 

More information on regular expressions

Using regular expressions with JavaScript

 

Good luck!

 

Glad to help further if you run into troubles.

well what I tried in the mean while was a loop that checks every character

so each time it passes a space, it dumps its buffer into an array, if it passes a quote, it ignores spaces till it finds another quote, and that all works incredibly well

but, the result of the entire thing is, elements are converted correctly and reassembled to Dom objects as they should, but I'd like to strip all the new lines and tabs from the tags, but doing something like =>if(tag.substr(i,1) == "\n" || tag.substr(i,1) == "\t" || tag.substr(i,1) == "")

 

but with no result, here is my splitTag function:

function splitTag(tag){
var list = new Array();
var buffer = "";
var ignoreSpace = false;
var ignoreNextChar = false;
var openedQuote = false;
for(var i = 0;i < tag.length;i++){
	if(tag.substr(i,1) == "\n" || tag.substr(i,1) == "\t" || tag.substr(i,1) == ""){
		//I do nothin'
	}
	else{
		if(!ignoreSpace && tag.substr(i,1) == " "){
			if(buffer.length > 0){
				list[list.length] = buffer;
				buffer = "";
			}
		}
		else{
			if(ignoreSpace && tag.substr(i,1) == " "){
				buffer += tag.substr(i,1);
			}
			else if(ignoreNextChar){
				buffer += tag.substr(i,1);
				ignoreNextChar = false;
			}
			else if(tag.substr(i,1) == "'" || tag.substr(i,1) == "\""){
				if(openedQuote == ""){
					openedQuote = tag.substr(i,1);
					ignoreSpace = true;
				}
				else{
					if(openedQuote == tag.substr(i,1)){
						openedQuote = "";
						ignoreSpace = false;
					}
					else{
						buffer += tag.substr(i,1);
					}
				}
			}
			else if(tag.substr(i,1) == "\\"){
				ignoreNextChar = true;
				buffer += tag.substr(i,1);
			}
			else{
				buffer += tag.substr(i,1);	
			}
		}
	}
}
if(buffer.length > 0){
	list[list.length] = buffer;
	buffer = "";
}
return list;
}

 

now how can I pull this last thing off,

because right now, if I put a newline in my source code, the resulting layout drops a line as well... and I don't want that

 

thx in advance

Thx for the code, it seems to work nicely, only it always skips the first tag, (like img in the example)

but all I still need right now, is a way to check that all special chars are filtered out, like linebreaks in the source code

for example, if my source has =>

<div id='div1'>
<div id='div2'>stuff here</div>
</div>

 

I will get a blank line before "stuff here"

if I have the source =>

<div id='div1'><div id='div2'>stuff here</div></div>

then I don't have a blank line

 

any solutions for this problem?

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.