保存 HTML 评论的内容

发布于 2024-12-20 09:14:03 字数 462 浏览 1 评论 0原文

我有一个页面,我想将 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

剑心龙吟 2024-12-27 09:14:04

不要使用正则表达式解析 html。使用xpath:

$dom = new DOMDocument();
@$dom->loadHTML($html);
$xpath = new DomXpath($dom);

foreach($xpath->query("//comment()") as $comment){
    echo $comment->nodeValue."\n";
}

Don't parse html with regex. Use xpath:

$dom = new DOMDocument();
@$dom->loadHTML($html);
$xpath = new DomXpath($dom);

foreach($xpath->query("//comment()") as $comment){
    echo $comment->nodeValue."\n";
}
孤君无依 2024-12-27 09:14:03

我知道很多人不喜欢正则表达式,但它们在这里可能会派上用场。尝试这样的话:

    $html = '<!--http://localhost/sfddsf.png-->';

    preg_match('/<!--([\S]+)-->/', $html, $matches);
    if ($matches[1])
       $url = $matches[1]; // should be http://localhost/sfddsf.png

祝你好运。

I know a lot of people rag on regular expressions, but they might come in handy here. Try something like:

    $html = '<!--http://localhost/sfddsf.png-->';

    preg_match('/<!--([\S]+)-->/', $html, $matches);
    if ($matches[1])
       $url = $matches[1]; // should be http://localhost/sfddsf.png

Good luck.

对不⑦ 2024-12-27 09:14:03

你的代码有点混乱:

  1. 你有向后搜索的字符串;使用 substr() ,应该是干草堆,起始位置,长度
  2. 您正在搜索错误的开始标记("),并且它们在参数列表中的位置与它应该是(开始,长度而不是最后,第一个,因为你看起来有)。
  3. 您没有搜索任何带有 getCurrentUrl() 返回值的 html 标记。

然而,下面的方法是有效的。但另请注意,如果您正在搜索的标记中有多个 html 注释,则此方法将不起作用。

<?php

$html = "
<html>
<head>
<!--http://localhost/sfddsf.png-->
</head>
<body></body>
</html>
";

echo "$html\n";
$strstart = strpos($html, "<!--") + 4;
$strend = strpos($html, "-->") - $strstart;
echo "$strstart, $strend\n";
$content = substr($html, $strstart, $strend);
print($content);

?>

http://codepad.org/3STPRsoj

打印:

<html>
<head>
<!--http://localhost/sfddsf.png-->
</head>
<body></body>
</html>

22, 27
http://localhost/sfddsf.png

Your code is a bit confused:

  1. You have the strings you're searching for backwards; using substr(), it should be haystack, start position, length.
  2. You're searching for the wrong opening tag ("<!--" instead of -->), and their position in the argument list is reversed from what it should be (start, length instead of last, first as it appears you have).
  3. You're not searching anything that even approximates an html tag with the return value of getCurrentUrl().

The below, however, works. Also note, however, this will not work if there are multiple html comments in your markup you're searching.

<?php

$html = "
<html>
<head>
<!--http://localhost/sfddsf.png-->
</head>
<body></body>
</html>
";

echo "$html\n";
$strstart = strpos($html, "<!--") + 4;
$strend = strpos($html, "-->") - $strstart;
echo "$strstart, $strend\n";
$content = substr($html, $strstart, $strend);
print($content);

?>

http://codepad.org/3STPRsoj

Which prints:

<html>
<head>
<!--http://localhost/sfddsf.png-->
</head>
<body></body>
</html>

22, 27
http://localhost/sfddsf.png
水溶 2024-12-27 09:14:03

不应该是:

$start = strpos($html, "<!--");
$end = strpos($html, "-->") + 3;
$content = substr($html, $start, $end - $start);

或者,如果您不想要 以及干净的字符串,您可以这样做:

$start = strpos($html, "<!--") + 4;
$end = strpos($html, "-->");
$content = trim(substr($html, $start, $end - $start));

Shouldn't it be:

$start = strpos($html, "<!--");
$end = strpos($html, "-->") + 3;
$content = substr($html, $start, $end - $start);

?

Or if you don't want the <!-- and the -->, and a clean string, you can do:

$start = strpos($html, "<!--") + 4;
$end = strpos($html, "-->");
$content = trim(substr($html, $start, $end - $start));
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文