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.

6
  • 1
    (related) Best Methods to parse HTML Commented Jun 1, 2011 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? Commented Jun 1, 2011 at 12:21
  • Nope, aside from after the "=", always the same. Commented Jun 1, 2011 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. Commented Jun 1, 2011 at 12:24
  • Thanks, but in this case it would have to be PHP. Commented Jun 1, 2011 at 12:26

2 Answers 2

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().

Sign up to request clarification or add additional context in comments.

3 Comments

Hmm. It doesn't seem to be working?
@Kobie: I tested it, and it works for me. Double-check that your question matches your actual situation.
Ahh. Figured it out. Thank you very much!
0

regular expression:

preg_match('#\<\-\-(.*)\=(.*)\-\-\>#', $html, $matches);
$matches[1] //contain data

1 Comment

The issue is that I need the "CLTAG GeographicArea=" included as well.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.