Solved

another PCRE question: need to match either display:none or visibility:hidden in the style attribute.

Posted on 2011-09-09
2
272 Views
Last Modified: 2012-06-21
hi there, the style attribute is retrieved using the php html dom class. i have used a regular expression to find either or, as in line 7 of the following function:
function ishidden($name,$html) {
	$str = str_get_html($html);
	$parent = $str->find("[name=".$name."]",0);
	while ($parent = $parent->parent()) {
		if (preg_match("/(display|visibility)\s*:\s*(none|hidden)/i",$parent->$style,$match)) {
			//print_r($parent);
			echo "style = ".$parent->$style."\n";
			return $match[0];
		}
	}
	return false;
}

function getforms($html) {
	/*
	- load form as:
	  array(
	   [type],
	   [name],
	   [value],
	   [checked],
	   [disabled]
	  );
	*/
	$ret = array();
	$forms = array();
	preg_match_all("/\<form [^\>]+\>.*\<\/form\>/ims",
			$html,
			$out);
	//print_r($out);
	foreach ($out[0] as $form) {
		array_push($forms,$form);
	}
	foreach ($forms as $form) {
		//echo $form."\n";
		$pattern = '/<(form)(?=(?:[^>]*name="([^"]*))?)(?=(?:[^>]*action="([^"]*))?)' .
				 '|<(input)(?=(?:[^>]*type="([^"]*))?)(?=(?:[^>]*name="([^"]*))?)(?=(?:[^>]*value="([^"]*))?)(?=(?:[^>]*checked="([^"]*))?)(?=(?:[^>]*disabled="([^"]*))?)' .
				 '|<(textarea)(?=(?:[^>]*name="([^"]*))?)[^>]*>([^<]|<(?!\/textarea))(?=(?:[^>]*disabled="([^"]*))?)' .
				 '|<(select)(?=(?:[^>]*name="([^"]*))?)(?=(?:[^>]*disabled="([^"]*))?)' .
				 '|<(button)(?=(?:[^>]*name="([^"]*))?)(?=(?:[^>]*disabled="([^"]*))?)/i';
		//$pattern = '/\<(form).*name="([^"]+)".*action="([^")".*\>'.
		//		 '|\<(input).*type="([^"]+)".*name="([^"]+)".*value="([^"]+)".*checked="([^"]+)".*\/\>'.
		//		 '|\<(textarea).*name="([^"]+)".*\>(.*)*\<\/textarea\>'.
		//		 '|<(select).*name="([^"]+)".*>'.
		//		 '|<(button).*name="([^"]+)".*value="([^"]+)".*>/ims';
		preg_match_all($pattern,
					$form,
					$out,
					PREG_SET_ORDER);
		$out = array_non_empty_items($out);
		$i = 0;
		foreach ($out as $element) {
			$ret[$i] = array();
			switch ($element[1]) {
				case "form":
					array_push($ret[$i], array(
										"type"	=>	$element[1],
										"name"	=>	$element[2],
										"action"	=>	$element[3]
									)
								);
					break;
				case "input":
					array_push($ret[$i], array(
										"input"	=>	$element[1],
										"type"	=>	$element[2],
										"name"	=>	$element[3],
										"value"	=>	$element[4],
										"checked"	=>	((($element[2]=="radio"||$element[2]=="checkbox")?true:false)?"true":"false"),
										"disabled"=>	(($element[2]=="radio"||$element[2]=="checkbox")?(($element[6]=="true"||$element[6]=="disabled")?true:false):(($element[5]=="true"||$element[5]=="disabled")?true:false))||(ishidden($element[3],$form))?"true":"false"
									)
								);
					print_r(ishidden($element[3],$form));
					break;
				case "textarea":
					array_push($ret[$i], array(
										"input"	=>	$element[1],
										"name"	=>	$element[2],
										"value"	=>	$element[3],
										"disabled"=>	($element[4]=="true"||$element[4]=="disabled"||ishidden($element[2],$form))?"true":"false"
									)
								);
					break;
				case "select":
					array_push($ret[$i], array(
										"input"	=>	$element[1],
										"name"	=>	$element[2],
										"value"	=>	getoption($element[2],$form),
										"disabled"=>	($element[4]=="true"||$element[4]=="disabled"||ishidden($element[2],$form))?"true":"false"
									)
								);
					break;
				case "button":
					array_push($ret[$i], array(
										"input"	=>	$element[1],
										"name"	=>	$element[2],
										"value"	=>	$element[3],
										"disabled"=>	($element[4]=="true"||$element[4]=="disabled"||ishidden($element[2],$form))?"true":"false"
									)
								);
					break;
			}
			$i++;
		}
	}
	return $ret;
}

Open in new window

what is wrong with this function or perl compatible regular expression that preg_match uses, that it does not return the expected results? 0o
the topmost parent is as follows within https://lmx.leads360.com/web/Login.aspx, as returned by file_get_contents.
<div id="passwordDialog" style="position:absolute;visibility:hidden;height:;width:;" class="dialog"><div id="passwordDialog_HeaderSpan" class="header">
	
            Forgot Password
        
</div><div id="passwordDialog_InnerSpan" class="content password">
	
            <p>Please enter the email address you signed up with and we will send you a new login link.</p>
            <dl>
                <dt>Email:</dt>
                <dd><input name="emailTextBox" type="text" id="emailTextBox" class="bigtextbox" /></dd>
            </dl>
            <div class="buttons submitcancel">
                <a class="submit left" id="submitPasswordRest" onclick="RequestPasswordReset();">Submit</a>
                <a class="cancel right" onclick="passwordDialog.Close();">Close</a>
            </div>
        
</div><div id="passwordDialog_FooterSpan" class="footer">
	
        
</div></div>

Open in new window

if anybody can see why this would not be returning true or the matched text in the function ishidden, even be it as result of it's usage - please let me know.
0
Comment
Question by:intellisource
  • 2
2 Comments
 

Accepted Solution

by:
intellisource earned 0 total points
ID: 36510067
lol nvm - misread a reference earlier to retrieving the style attributes as $parent->$style instead of $parent->getAttribute('style') ;)
0
 

Author Closing Comment

by:intellisource
ID: 36510078
working now! i can get the form elements as an array, choose which ones need to be "displayed" for submission - and then compile the POST str ;)
perserverance is KEY! XD
0

Featured Post

NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to count occurrences of each item in an array.

822 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question