简体   繁体   English

正则表达式-需要从html注释获取字符串

[英]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. 我需要从HTML文件中的注释中获取字符串,我试图使用DOM进行处理,但是我没有找到使用此方法的好的解决方案。

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 预先感谢您的回答

在此代码后查看$ matches

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

HTML comments are regular; HTML注释是常规的; you can just match <!--adress-"([^">]+)"--> and get the first group. 您可以匹配<!--adress-"([^">]+)"-->并获得第一组。

This assumes that the comments are always well-formed and always have a quoted string containing no quotes. 这假定注释始终是格式正确的,并且始终具有不带引号的带引号的字符串。

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. 只需执行var_dump($matches_array)并查看结果。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM