title: Get HTML page <input> values and names using regex on PHP
body: <p>Ok, so as the title says, I have an HTML page that I fetch using libcurl (cURL inside PHP).<br />
  That page has one &lt;form> that I need to extract the &lt;input> names and values, and I would like to do that using Regex.<br/>
  I'm making it using Regex because I think that's the easier way. If you think I shouldn't use regex, but something like xpath, say how.</p>
  
  <p>I don't know if you can understand what I'm trying to say, so feel free to ask.</p>
  
  <p>Here's the PHP code (complete):<br /></p>
  
  <pre><code>&lt;?php
  
  
  
  /***** DISABLED BY NETWORK TRAFFIC REASONS... USING LOCAL CACHE
  
  $curl = curl_init();
  $url = 'https://secure.optimus.pt/Particulares/Kanguru/Login/';
  $useragent = 'Mozilla/5.0 (Windows; U; Windows NT 6.0; pt-PT; rv:1.9.1) Gecko/20090624 Firefox/3.5';
  curl_setopt($curl,CURLOPT_URL,$url);
  curl_setopt($curl,CURLOPT_USERAGENT,$useragent);
  curl_setopt($curl,CURLOPT_SSL_VERIFYPEER,true);
  curl_setopt($curl,CURLOPT_SSL_VERIFYHOST,2);
  curl_setopt($curl,CURLOPT_CAINFO,getcwd()."\optimus_secure.crt");
  curl_setopt($curl,CURLOPT_RETURNTRANSFER,true);
  $contents = curl_exec($curl);
  */
  
  $contents = file_get_contents('local_secure.html');
  preg_match('%&lt;form name="aspnetForm" .*? action="(.*?)" .*?&gt;(.*?)&lt;/form&gt;%s',$contents,$matches);
  //echo '&lt;pre&gt;'.htmlentities($contents).'&lt;/pre&gt;';
  //array_shift($matches);
  echo '&lt;pre&gt;---------';
  foreach($matches as $match)
      echo '$match:::::: '.htmlentities($match)."\r\n\r\n";
  echo '&lt;/pre&gt;';
  
  echo '&lt;pre&gt;__________';
  preg_match_all('/&lt;input type=".*?" name="(.*?)" value="(.*?)" \/&gt;/', $matches[0], $matches2);
  print_r($matches2);
  echo '&lt;/pre&gt;';
  
  ?&gt;
  </code></pre>
  
  <p>Of course that the &lt;pre> tags and all that output is just for debugging.</p>
  
  <p>Also, here's the source code of the HTML page (the part that matters):</p>
  
  <pre><code>&lt;form name="aspnetForm" method="post" action="../Login?OptimusChannelID=D5774383-A407-42E9-A0AD-4838C97AB162&amp;amp;OptimusContentID=&amp;amp;OptimusSelectedSiteID=B33E7D52-8738-4756-A25D-B907D1823B71&amp;amp;OptimusSelectedAreaID=AF8E0BDF-17E3-4438-9FA9-D53A13A508D8&amp;amp;OptimusSelectedLocalID=D5774383-A407-42E9-A0AD-4838C97AB162" onsubmit="javascript:return WebForm_OnSubmit();" id="aspnetForm"&gt;
  &lt;div&gt;
  &lt;input type="hidden" name="__EVENTTARGET" id="__EVENTTARGET" value="" /&gt;
  &lt;input type="hidden" name="__EVENTARGUMENT" id="__EVENTARGUMENT" value="" /&gt;
  &lt;input type="hidden" name="__VIEWSTATE" id="__VIEWSTATE" value="/wEPDwUKLTc4MzE4NTQyNQ9kFgJmD2QWBgIID2QWAgIBD2QWBGYPZBYCAgMPDxYEHhRWYWxpZGF0aW9uRXhwcmVzc2lvbgUCLioeB0VuYWJsZWRoZGQCAQ9kFgICBQ8PFgIeBFRleHQFKk8gY2FtcG8gRW1haWwgJmVhY3V0ZTsgb2JyaWdhdCZvYWN1dGU7cmlvIWRkAgkPZBYCAgEPFgIfAmVkAgoPDxYCHgdWaXNpYmxlaGRkGAEFHl9fQ29udHJvbHNSZXF1aXJlUG9zdEJhY2tLZXlfXxYCBSVjdGwwMCRNYWluQ29udGVudFBsYWNlSG9sZGVyJEltZ0xvZ2luBSxjdGwwMCRNYWluQ29udGVudFBsYWNlSG9sZGVyJGltZ0J0blJlY3VwZXJhcorZDETv8JCxlvTojv3w53/dbo9m" /&gt;
  &lt;/div&gt;
  &lt;script type="text/javascript"&gt;....&lt;/script&gt;
  &lt;script src="..." type="text/javascript"&gt;&lt;/script&gt;
  &lt;script src="..." type="text/javascript"&gt;&lt;/script&gt;
  &lt;script type="text/javascript"&gt;...&lt;/script&gt;
  &lt;div class="row_container"&gt;
  &lt;div class="titulo_barra rosa laranja_empresas"&gt;
  LOGIN&lt;br/&gt;
  &lt;/div&gt;
  &lt;div class="PanelLogin"&gt;
  &lt;div class="Mensagem"&gt;
  &lt;div class="texto"&gt;
  Para aceder, por favor, fa&amp;ccedil;a login. 
  &lt;/div&gt;
  &lt;/div&gt;
  &lt;div id="ctl00_MainContentPlaceHolder_PanelLogin" onkeypress="javascript:return WebForm_FireDefaultButton(event, 'ctl00_MainContentPlaceHolder_ImgLogin')"&gt;
  &lt;div class="Mensagem"&gt;
  &lt;div&gt;                           
  &lt;label for="telem"&gt;
  User&lt;span id="ctl00_MainContentPlaceHolder_UsernameValidator" style="color:Red;display:none;"&gt;&lt;/span&gt;
  &lt;/label&gt;
  &lt;input name="ctl00$MainContentPlaceHolder$TxtUsername" type="text" id="ctl00_MainContentPlaceHolder_TxtUsername" class="text" maxlength="255" /&gt;
  &lt;label style="padding-left: 10px" for="password"&gt;
  Password
  &lt;span id="ctl00_MainContentPlaceHolder_RequiredPasswordValidator" style="color:Red;display:none;"&gt;&lt;/span&gt;&lt;span id="ctl00_MainContentPlaceHolder_UsernameRegexValidator" style="color:Red;display:none;"&gt;&lt;/span&gt; &lt;/label&gt;
  &lt;input name="ctl00$MainContentPlaceHolder$TxtPassword" type="password" id="ctl00_MainContentPlaceHolder_TxtPassword" class="text" maxlength="5" /&gt;
  &lt;input type="hidden" name="fromssl" value="" /&gt;
  &lt;input type="image" name="ctl00$MainContentPlaceHolder$ImgLogin" id="ctl00_MainContentPlaceHolder_ImgLogin" src="/img/btn_password.gif" alt="Login" onclick="javascript:WebForm_DoPostBackWithOptions(new WebForm_PostBackOptions(&amp;quot;ctl00$MainContentPlaceHolder$ImgLogin&amp;quot;, &amp;quot;&amp;quot;, true, &amp;quot;&amp;quot;, &amp;quot;&amp;quot;, false, false))" style="border-width:0px;position: absolute; padding-left: 5px " /&gt;&lt;br /&gt;                          
  &lt;/div&gt;
  &lt;div id="login_error_box"&gt;
  &lt;div id="ctl00_MainContentPlaceHolder_ValidationSummary1" class="error" style="color:#FF6000;display:none;"&gt;
  &lt;/div&gt;
  &lt;/div&gt;
  &lt;/div&gt;
  &lt;/div&gt;
  &lt;/div&gt;
  &lt;div class="titulo_barra rosa laranja_empresas"&gt;
  RECUPERA&amp;Ccedil;&amp;Atilde;O DE PASSWORD
  &lt;/div&gt;
  &lt;div class="PanelLogin"&gt;
  &lt;div class="Mensagem"&gt;
  &lt;div class="texto"&gt;
  Para recuperar a sua password introduza o seu e-mail. Se pretender recuperar o seu username utilize o link abaixo
  &lt;/div&gt;
  &lt;/div&gt;
  &lt;div id="ctl00_MainContentPlaceHolder_Panel1" onkeypress="javascript:return WebForm_FireDefaultButton(event, 'ctl00_MainContentPlaceHolder_imgBtnRecuperar')"&gt;
  &lt;div class="Mensagem"&gt;
  &lt;div id="Div1"&gt;
  &lt;label for="telem"&gt;
  Email&lt;/label&gt;
  &lt;input name="ctl00$MainContentPlaceHolder$txtEmailHabitual" type="text" id="ctl00_MainContentPlaceHolder_txtEmailHabitual" class="text" maxlength="255" /&gt;
  &lt;input type="image" name="ctl00$MainContentPlaceHolder$imgBtnRecuperar" id="ctl00_MainContentPlaceHolder_imgBtnRecuperar" class="img rosa azul_empresas" src="/img/bot_recuperar.gif" alt="Recuperar" onclick="javascript:WebForm_DoPostBackWithOptions(new WebForm_PostBackOptions(&amp;quot;ctl00$MainContentPlaceHolder$imgBtnRecuperar&amp;quot;, &amp;quot;&amp;quot;, true, &amp;quot;email&amp;quot;, &amp;quot;&amp;quot;, false, false))" style="border-width:0px;margin-top: -2px; position: absolute;" /&gt;
  &lt;br /&gt;
  &lt;span id="ctl00_MainContentPlaceHolder_EmailValidator" class="error" style="color:Red;display:none;"&gt;O campo Email &amp;eacute; obrigat&amp;oacute;rio!&lt;/span&gt;
  &lt;span id="ctl00_MainContentPlaceHolder_EmailRegularExpressionValidator" style="color:Red;display:none;"&gt; Formato do Email inválido.&lt;/span&gt;
  &lt;/div&gt; 
  &lt;div class="Mensagem" CssClass="error" DisplayMode="SingleParagraph" ForeColor="#FF6000"&gt;
  &lt;/div&gt;
  &lt;a id="ctl00_MainContentPlaceHolder_lnkRecuser" href="javascript:__doPostBack('ctl00$MainContentPlaceHolder$lnkRecuser','')"&gt;
  &lt;div align="left"  style="color:#FF7000" class="footerButtonsOrange"&gt;Recuperar username&lt;/div&gt;
  &lt;/a&gt;
  &lt;/div&gt;
  &lt;/div&gt;
  &lt;/div&gt;
  &lt;/div&gt;
  &lt;script type="text/javascript"&gt;...&lt;/script&gt;
  &lt;script type="text/javascript"&gt;...&lt;/script&gt;
  &lt;script type="text/javascript"&gt;...&lt;/script&gt;
  &lt;div&gt;
  &lt;input type="hidden" name="__EVENTVALIDATION" id="__EVENTVALIDATION" value="/wEWBwKQ08lZAqmxyPwLAvCnm8wMAt/Wt8sGAv2svvMEAtCB5oUIAr6ar9wLz+9apOkY23Vs+vCYNJuK2ug3Gm0=" /&gt;
  &lt;/div&gt;
  &lt;script type="text/javascript"&gt;...&lt;/script&gt;
  &lt;/form&gt;
  </code></pre>
  
  <p>Also, sorry for the low readability of the source code. If you want, I can try to indent it better.</p>
  
  <p>Thank you,<br />
  Pedro Cunha</p>
  
  <p><p>EDIT: Thank you all for your help. All the answers worked flawlessly, however I chose VolkerK's response, because since it is an HTML page, elements may be nested, and I know (of the few things that I know about XPath) that // is a wildcard.</p>