Lukáš Jelič
Lukáš Jelič

Reputation: 559

Regular expression - need to get string from html comment

I need to get string from comment in HTML file, I was trying to do it with DOM, but I didn't find good solution with this method.

So I want to try it with regular expressions, but I can't find satisfactory solution. Please, can you help me?

This is what I need:

<!--adress-"String here I need to get"-->

Thanks in advance for answer

Upvotes: 2

Views: 1217

Answers (3)

Vladimir Fesko
Vladimir Fesko

Reputation: 222

It will be more accurate:

$regex = '<!--(.+?)-"{0,1}(.+?)"{0,1}-->';
preg_match_all($regex, $html, $matches_array);

Just do the var_dump($matches_array) and see results.

Upvotes: 1

SLaks
SLaks

Reputation: 887245

HTML comments are regular; you can just match <!--adress-"([^">]+)"--> and get the first group.

This assumes that the comments are always well-formed and always have a quoted string containing no quotes.

Upvotes: 1

genesis
genesis

Reputation: 50966

Look into $matches after this code

preg_match('~<!--adress-"(.*?)"-->~msi', $string, $matches);

Upvotes: 4

Related Questions