0

Possible Duplicate:
find and edit comment element of html with php

Hello. Regular expressions in general are way over my head. The answer to my question should be fairly simple, but after digging around for a while I'm still at loss for one. How can I extract "Orange County, CA" using preg_match() in PHP from this HTML excerpt?:

<!-- CLTAG GeographicArea=Orange County, CA -->

Note that the text in this area will be variable and thus the regular expression needs to be dynamic to account for this.

Community
  • 1
  • 1
delaccount992
  • 1,303
  • 3
  • 15
  • 21
  • 1
    *(related)* [Best Methods to parse HTML](http://stackoverflow.com/questions/3577641/best-methods-to-parse-html/3577662#3577662) – Gordon Jun 01 '11 at 12:20
  • Can the HTML comment take any other form (apart from the variable text after the `=`) or will it only ever be exactly like this? – Tomalak Jun 01 '11 at 12:21
  • Nope, aside from after the "=", always the same. – delaccount992 Jun 01 '11 at 12:23
  • I can't help you with preg_match() in PHP but there is a RegExp() function in Javascript that takes dynamic regular expressions...Maybe it could help. – Krimo Jun 01 '11 at 12:24
  • Thanks, but in this case it would have to be PHP. – delaccount992 Jun 01 '11 at 12:26
  • Modify the XPath in the linked duplicate to `//comment()[contains(., "CLTAG GeographicArea=")]'`. Everything else is the same. – Gordon Jun 01 '11 at 12:33

2 Answers2

3

One regex you can use would be this:

$matches = array();
preg_match('/<!-- CLTAG GeographicArea=(.*?) -->/', $html, $matches);

echo $matches[1];

If there are multiple such comments in the HTML, use preg_match_all().

Tomalak
  • 332,285
  • 67
  • 532
  • 628
0

regular expression:

preg_match('#\<\-\-(.*)\=(.*)\-\-\>#', $html, $matches);
$matches[1] //contain data
Adrian B
  • 1,490
  • 1
  • 19
  • 31