保存 HTML 评论的内容
我有一个页面,我想将 html 注释的注释保存为变量:
<!--http://localhost/sfddsf.png-->
How do i only get the content of the html comment?我搜索了几个答案,但没有用。
function getCurrentUrl(){
$domain = $_SERVER['HTTP_HOST'];
$url = "http://" . $domain . $_SERVER['REQUEST_URI'];
return $url;
}
$html = getCurrentUrl();
$content = substr($html, strpos($html, "-->"), strpos($html, "<--"));
print_r( $content);
I have a page and i wanted to save the comment of an html comment as a variable:
<!--http://localhost/sfddsf.png-->
How do i only get the content of the html comment? I searched for a couple of answers but it's not working.
function getCurrentUrl(){
$domain = $_SERVER['HTTP_HOST'];
$url = "http://" . $domain . $_SERVER['REQUEST_URI'];
return $url;
}
$html = getCurrentUrl();
$content = substr($html, strpos($html, "-->"), strpos($html, "<--"));
print_r( $content);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
不要使用正则表达式解析 html。使用xpath:
Don't parse html with regex. Use xpath:
我知道很多人不喜欢正则表达式,但它们在这里可能会派上用场。尝试这样的话:
祝你好运。
I know a lot of people rag on regular expressions, but they might come in handy here. Try something like:
Good luck.
你的代码有点混乱:
substr()
,应该是干草堆,起始位置,长度
。"
),并且它们在参数列表中的位置与它应该是(开始,长度
而不是最后,第一个
,因为你看起来有)。getCurrentUrl()
返回值的 html 标记。然而,下面的方法是有效的。但另请注意,如果您正在搜索的标记中有多个 html 注释,则此方法将不起作用。
http://codepad.org/3STPRsoj
打印:
Your code is a bit confused:
substr()
, it should behaystack, start position, length
."<!--"
instead of-->
), and their position in the argument list is reversed from what it should be (start, length
instead oflast, first
as it appears you have).getCurrentUrl()
.The below, however, works. Also note, however, this will not work if there are multiple html comments in your markup you're searching.
http://codepad.org/3STPRsoj
Which prints:
不应该是:
?
或者,如果您不想要
以及干净的字符串,您可以这样做:
Shouldn't it be:
?
Or if you don't want the
<!--
and the-->
, and a clean string, you can do: