Regex help please - get name/value from html

I’m trying to get the name/value information from input tags in a form. The problem is that the tags are written in unpredictable formats, sometimes the value isn’t specified…


<input type="text" name="thename" value="the value">
or
<input type="text" name='thename' value="the value">
or
<input type="text" NAME="thename" ID="myid" VALUE='the value'>
or
<input type="text" name="thename">

I’ve come up with the following regular expression:


/name=['|\\"]([\\w _]+)['|\\"].*value=['|\\"](.*?)['|\\"]/i

It gets the name consistently and will get the name/value when they are both in the tag. It doesn’t get anything when value=“…” is missing. I’ve tried everything I can think of on value='|\"['|\"] to make it optional but nothing I do works.

Any help would be greatly appreciated

This works great! You wouldn’t know how to add an input field to the form I’m testing on using the DOM here would you?

Thanks! I’ll give that a shot.

can’t you check for an empty value first and supply a default before performing the reg ex and then strip it out when needed, or am i missing the point

I’m getting this information from another page, reading it in using file and then parsing it…

Why not use DOM or SimpleXML ?

Sure, that’s not an issue.


<?php
$doc = new DOMDocument();
@$doc->loadHTMLFile('http://www.sitepoint.com/');
if(false !== $doc){
  foreach($doc->getElementsByTagName('a') as $a){
    if($a->hasAttribute('href')){
      printf('&#37;s<br />', $a->getAttribute('href'));
    }
  }
}
/*
  http://www.sitepoint.com/
  http://www.sitepoint.com/
  http://www.sitepoint.com/blogs/
  http://www.sitepoint.com/recentarticles/
  http://www.sitepoint.com/books/
  http://www.sitepoint.com/kits/
  http://www.sitepoint.com/videos/
  http://marketplace.sitepoint.com/
  http://www.sitepoint.com/forums/
  http://reference.sitepoint.com/
  http://articles.sitepoint.com/article/silverlight-php-twitter/
  http://www.sitepoint.com/forums/showthread?t=687989
  http://articles.sitepoint.com/article/silverlight-php-twitter/
  http://articles.sitepoint.com/article/silverlight-php-twitter/
  /webslices/news.php
  http://www.sitepoint.com/blogs/2010/07/09/handling-client-data
  http://www.sitepoint.com/blogs/2010/07/09/handling-client-data
  http://www.sitepoint.com/blogs/2010/07/09/handling-client-data
  http://www.sitepoint.com/blogs/2010/07/08/creating-a-simple-twitter-client-with-silverlight-and-php
  http://www.sitepoint.com/blogs/2010/07/08/creating-a-simple-twitter-client-with-silverlight-and-php
  http://www.sitepoint.com/blogs/2010/07/08/creating-a-simple-twitter-client-with-silverlight-and-php#comments
  http://www.sitepoint.com/blogs/2010/07/08/creating-a-simple-twitter-client-with-silverlight-and-php
  http://www.sitepoint.com/blogs/2010/07/08/notes-on-the-care-and-feeding-of-freelancers
  http://www.sitepoint.com/blogs/2010/07/08/notes-on-the-care-and-feeding-of-freelancers
  http://www.sitepoint.com/blogs/2010/07/08/notes-on-the-care-and-feeding-of-freelancers
  http://www.sitepoint.com/blogs/2010/07/08/4-twitter-tools-for-followers
  http://www.sitepoint.com/blogs/2010/07/08/4-twitter-tools-for-followers
  http://www.sitepoint.com/blogs/2010/07/08/4-twitter-tools-for-followers
  http://www.sitepoint.com/blogs/2010/07/07/opera-10-60-released
  http://www.sitepoint.com/blogs/2010/07/07/opera-10-60-released
  http://www.sitepoint.com/blogs/2010/07/07/opera-10-60-released#comments
  http://www.sitepoint.com/blogs/2010/07/07/opera-10-60-released
  http://www.sitepoint.com/blogs/2010/07/06/how-to-use-the-new-custom-menu-feature-in-wordpress-3-0
  http://www.sitepoint.com/blogs/2010/07/06/how-to-use-the-new-custom-menu-feature-in-wordpress-3-0
  http://www.sitepoint.com/blogs/2010/07/06/how-to-use-the-new-custom-menu-feature-in-wordpress-3-0#comments
  http://www.sitepoint.com/blogs/2010/07/06/how-to-use-the-new-custom-menu-feature-in-wordpress-3-0
  http://www.sitepoint.com/blogs/2010/07/06/canvas-vs-svg-how-to-choose
  http://www.sitepoint.com/blogs/2010/07/06/canvas-vs-svg-how-to-choose
  http://www.sitepoint.com/blogs/2010/07/06/canvas-vs-svg-how-to-choose#comments
  http://www.sitepoint.com/blogs/2010/07/06/canvas-vs-svg-how-to-choose
  /blogs/
  http://articles.sitepoint.com/article/photography-for-the-web-composition/
  http://www.sitepoint.com/forums/showthread?t=687788
  http://articles.sitepoint.com/article/photography-for-the-web-composition/
  http://articles.sitepoint.com/article/photography-for-the-web-composition/
  /webslices/design.php
  http://www.sitepoint.com/forums/showthread?t=687720
  http://www.sitepoint.com/forums/showthread?t=687720
  http://www.sitepoint.com/forums/showthread?t=687720
  http://www.sitepoint.com/blogs/2010/07/04/red-cover-re-design-for-penguin
  http://www.sitepoint.com/blogs/2010/07/04/red-cover-re-design-for-penguin
  http://www.sitepoint.com/blogs/2010/07/04/red-cover-re-design-for-penguin#comments
  http://www.sitepoint.com/forums/showthread?t=687217
  http://www.sitepoint.com/forums/showthread?t=687217
  http://www.sitepoint.com/forums/showthread?t=687217
  http://www.sitepoint.com/blogs/2010/07/03/twitter-meets-3d-in-robyns-online-video
  http://www.sitepoint.com/blogs/2010/07/03/twitter-meets-3d-in-robyns-online-video
  http://www.sitepoint.com/blogs/2010/07/03/twitter-meets-3d-in-robyns-online-video#comments
  http://www.sitepoint.com/blogs/2010/07/02/visual-harmony-in-design-part-3-thematic-reference
  http://www.sitepoint.com/blogs/2010/07/02/visual-harmony-in-design-part-3-thematic-reference
  http://www.sitepoint.com/blogs/2010/07/02/visual-harmony-in-design-part-3-thematic-reference#comments
  http://www.sitepoint.com/forums/showthread?t=686674
  http://www.sitepoint.com/forums/showthread?t=686674
  http://www.sitepoint.com/forums/showthread?t=686674
  http://www.sitepoint.com/blogs/2010/06/30/8-free-stylish-fonts-from-typographers-on-behance
  http://www.sitepoint.com/blogs/2010/06/30/8-free-stylish-fonts-from-typographers-on-behance
  http://www.sitepoint.com/blogs/2010/06/30/8-free-stylish-fonts-from-typographers-on-behance#comments
  http://www.sitepoint.com/forums/showthread?t=686399
  http://www.sitepoint.com/forums/showthread?t=686399
  http://www.sitepoint.com/forums/showthread?t=686399
  http://www.sitepoint.com/blogs/category/design/
  http://www.sitepoint.com/blogs/2010/07/08/mozilla-firefox-apple-iphone
  http://www.sitepoint.com/blogs/2010/07/08/mozilla-firefox-apple-iphone#comments
  http://www.sitepoint.com/blogs/2010/07/08/mozilla-firefox-apple-iphone
  http://www.sitepoint.com/blogs/2010/07/08/mozilla-firefox-apple-iphone
  /webslices/tech.php
  http://www.sitepoint.com/blogs/2010/07/08/creating-a-simple-twitter-client-with-silverlight-and-php
  http://www.sitepoint.com/blogs/2010/07/08/creating-a-simple-twitter-client-with-silverlight-and-php
  http://www.sitepoint.com/blogs/2010/07/08/creating-a-simple-twitter-client-with-silverlight-and-php#comments
  http://www.sitepoint.com/forums/showthread?t=687989
  http://www.sitepoint.com/forums/showthread?t=687989
  http://www.sitepoint.com/forums/showthread?t=687989
  http://www.sitepoint.com/blogs/2010/07/07/opera-10-60-released
  http://www.sitepoint.com/blogs/2010/07/07/opera-10-60-released
  http://www.sitepoint.com/blogs/2010/07/07/opera-10-60-released#comments
  http://www.sitepoint.com/blogs/2010/07/06/how-to-use-the-new-custom-menu-feature-in-wordpress-3-0
  http://www.sitepoint.com/blogs/2010/07/06/how-to-use-the-new-custom-menu-feature-in-wordpress-3-0
  http://www.sitepoint.com/blogs/2010/07/06/how-to-use-the-new-custom-menu-feature-in-wordpress-3-0#comments
  http://www.sitepoint.com/blogs/2010/07/06/canvas-vs-svg-how-to-choose
  http://www.sitepoint.com/blogs/2010/07/06/canvas-vs-svg-how-to-choose
  http://www.sitepoint.com/blogs/2010/07/06/canvas-vs-svg-how-to-choose#comments
  http://www.sitepoint.com/blogs/2010/07/05/microsoft-office-online-word-excel-powerpoint-onenote
  http://www.sitepoint.com/blogs/2010/07/05/microsoft-office-online-word-excel-powerpoint-onenote
  http://www.sitepoint.com/blogs/2010/07/05/microsoft-office-online-word-excel-powerpoint-onenote#comments
  http://www.sitepoint.com/forums/showthread?t=687259
  http://www.sitepoint.com/forums/showthread?t=687259
  http://www.sitepoint.com/forums/showthread?t=687259
  http://www.sitepoint.com/blogs/2010/07/02/shorter-javascript-function-statement
  http://www.sitepoint.com/blogs/2010/07/02/shorter-javascript-function-statement
  http://www.sitepoint.com/blogs/2010/07/02/shorter-javascript-function-statement#comments
  http://www.sitepoint.com/blogs/category/tech/
  http://www.sitepoint.com/blogs/2010/07/07/4-elements-of-trust-for-collaboration
  http://www.sitepoint.com/blogs/2010/07/07/4-elements-of-trust-for-collaboration
  http://www.sitepoint.com/blogs/2010/07/07/4-elements-of-trust-for-collaboration
  /webslices/business.php
  http://www.sitepoint.com/blogs/2010/07/08/notes-on-the-care-and-feeding-of-freelancers
  http://www.sitepoint.com/blogs/2010/07/08/notes-on-the-care-and-feeding-of-freelancers
  http://www.sitepoint.com/blogs/2010/07/08/4-twitter-tools-for-followers
  http://www.sitepoint.com/blogs/2010/07/08/4-twitter-tools-for-followers
  http://www.sitepoint.com/blogs/2010/07/06/facebook-fan-page-apps
  http://www.sitepoint.com/blogs/2010/07/06/facebook-fan-page-apps
  http://www.sitepoint.com/blogs/2010/07/06/facebook-fan-page-apps#comments
  http://www.sitepoint.com/blogs/2010/07/03/solution-to-social-media-noise
  http://www.sitepoint.com/blogs/2010/07/03/solution-to-social-media-noise
  http://www.sitepoint.com/blogs/2010/07/03/solution-to-social-media-noise#comments
  http://www.sitepoint.com/blogs/2010/07/02/benefits-of-guest-blogging
  http://www.sitepoint.com/blogs/2010/07/02/benefits-of-guest-blogging
  http://www.sitepoint.com/blogs/2010/07/02/benefits-of-guest-blogging#comments
  http://www.sitepoint.com/forums/showthread?t=686859
  http://www.sitepoint.com/forums/showthread?t=686859
  http://www.sitepoint.com/forums/showthread?t=686859
  http://www.sitepoint.com/blogs/2010/07/01/the-two-ingredients-of-successful-freelancing
  http://www.sitepoint.com/blogs/2010/07/01/the-two-ingredients-of-successful-freelancing
  http://www.sitepoint.com/blogs/2010/07/01/the-two-ingredients-of-successful-freelancing#comments
  http://www.sitepoint.com/blogs/2010/07/01/how-to-revamp-your-marketing-perspective
  http://www.sitepoint.com/blogs/2010/07/01/how-to-revamp-your-marketing-perspective
  http://www.sitepoint.com/blogs/category/business/
  http://www.twitter.com/sitepointdotcom
  http://www.facebook.com/sitepoint
  http://www.sitepoint.com/launch/54ac38/3/157/
  http://www.sitepoint.com/books/phpmysql4/
  http://www.sitepoint.com/launch/54ac38/2/157/
  http://www.sitepoint.com/launch/54ac38/3/157/
  https://sitepoint.com/bookstore/go/157
  http://www.sitepoint.com/books/html2/
  http://www.sitepoint.com/launch/693c9b/2/145/
  http://www.sitepoint.com/launch/693c9b/3/145/
  https://sitepoint.com/bookstore/go/145
  http://www.sitepoint.com/launch/012379/3/144/
  http://www.sitepoint.com/launch/012379/2/144/
  http://www.sitepoint.com/launch/012379/3/144/
  https://sitepoint.com/bookstore/go/144
  http://www.sitepoint.com/launch/7771f8/3/83/
  http://www.sitepoint.com/books/design1/
  http://www.sitepoint.com/launch/7771f8/2/83/
  http://www.sitepoint.com/launch/7771f8/3/83/
  https://sitepoint.com/bookstore/go/83
  /webslices/whatshot.php
  http://articles.sitepoint.com/article/intro-business-catalyst/
  http://articles.sitepoint.com/article/intro-business-catalyst/
  http://www.sitepoint.com/forums/showthread?t=686859
  http://www.sitepoint.com/blogs/2010/06/15/top-10-web-design-mistakes
  http://www.sitepoint.com/blogs/2010/06/15/top-10-web-design-mistakes
  http://www.sitepoint.com/blogs/2010/06/15/top-10-web-design-mistakes#comments
  http://articles.sitepoint.com/article/reseller-hosting-demystified-2010/
  http://articles.sitepoint.com/article/reseller-hosting-demystified-2010/
  http://www.sitepoint.com/forums/showthread?t=672308
  http://www.sitepoint.com/blogs/2010/06/22/using-a-soft-sales-approach
  http://www.sitepoint.com/blogs/2010/06/22/using-a-soft-sales-approach
  http://www.sitepoint.com/blogs/2010/06/22/using-a-soft-sales-approach#comments
  http://www.sitepoint.com/forums/showthread?t=674721
  http://www.sitepoint.com/forums/showthread?t=674721
  http://www.sitepoint.com/forums/showthread?t=674721
  http://www.sitepoint.com/about/terms
  http://articles.sitepoint.com/?source=homepage
  http://www.sitepoint.com/blogs?source=homepage
  http://reference.sitepoint.com?source=homepage
  http://www.sitepoint.com/newsletters?source=homepage
  http://www.sitepoint.com/books?source=homepage
  http://www.sitepoint.com/videos?source=homepage
  http://tools.sitepoint.com?source=homepage
  http://www.sitepoint.com/about/writeforus?source=homepage
  http://www.sitepoint.com/forums/register.php?source=homepage
  http://www.sitepoint.com/podcast?source=homepage
  http://www.sitepoint.com/newsletter?source=homepage
  http://www.sitepoint.com/subscribe?source=homepage
  http://marketplace.sitepoint.com?source=homepage
  http://m.sitepoint.com?source=homepage
  http://www.sitepoint.com/about?source=homepage
  http://www.sitepoint.com/about/jobs?source=homepage
  http://www.sitepoint.com/contact?source=homepage
  http://www.sitepoint.com/mediakit?source=homepage
  http://search.sitepoint.com?source=homepage
  http://www.sitepoint.com/sitemap?source=homepage
  http://www.twitter.com/sitepointdotcom
  http://www.facebook.com/sitepoint
  http://www.sitepoint.com.au?source=blogs
  http://www.99designs.com?source=blogs
  #sitepoint-logo
*/
?>

The regex was looking to see if the value attribute existed or not, and getting the value if it did exist.