I have html code including elements. What I am trying to do is, I need the whole html code of this form element. For example, in the html code below
...
<p>Sample</p>
<img src="..." />
<form method="post" >
<input type="hidden" value="v1" id="v1" name="task">
<input type="hidden" value="v2" name="v2">
...
</form开发者_JS百科>
<div>...</div>
...
I want to extract these codes:
<form method="post" >
<input type="hidden" value="v1" id="v1" name="task">
<input type="hidden" value="v2" name="v2">
...
</form>
Since I am not so familiar with preg_match expression, I hardly can figure it out. I googled to understand expressions myself, but only could get small portion of grasp.
Can any one help me, please? Best regards.
The regular expession to match the form tag may look like this: "(?smi)<form.*?</form>
"
EDIT 1: In PHP the function call will look like this: preg_match('/^.*?<form.*?<\/form>.*$/smi', $data)
EDIT 2: This can be tested here: http://www.spaweditor.com/scripts/regex/index.php
But in general case I wouldn't advise as well to use regular expressions for parsing HTML code.
For something as trivial as matching a form tag in html, just don't use regular expressions or third party xhtml parsers.
Use the the default DOM Parser instead.
It's as simple as :
// Create a new DOM Document to hold our webpage structure
$xml = new DOMDocument();
// Load the html's contents into DOM
$xml->loadHTML($html);
$forms = array();
//Loop through each <form> tag in the dom and add it to the $forms array
foreach($xml->getElementsByTagName('form') as $form) {
//Get the node's html string
$forms[] = $form->ownerDocument->saveXML($form);
}
where $forms
is an array of string of every forms.
Using regular expressions to handle HTML is generally not a good idea. I'd rather suggest to use a HTML parser. I had good results with this library: http://simplehtmldom.sourceforge.net/
精彩评论