gpt4 book ai didi

php正则表达式问题

转载 作者:行者123 更新时间:2023-12-02 08:52:12 24 4
gpt4 key购买 nike

是的,我知道在 html 上使用正则表达式不是首选,但我仍然对为什么这不起作用感到困惑:

我正在尝试从文档中删除“头部”。
这是文档:

<html>
<head>
<!--
a comment within the head
-->
</head>
<body>
stuff in the body
</body>
</html>

我的代码:

$matches = array(); $result = preg_match ('/(?:<head[^>]*>)(.*?)(<\/head>)/is', $contents, $matches); 
var_dump ($matches);

这实际上不起作用。这是我看到的输出:

array(3) { [0]=> string(60) " " [1]=> string(47) " " [2]=> string(7) "" }

但是,如果我将 HTMl 文档调整为没有评论

我错过了什么?

谢谢!

最佳答案

您的正则表达式看起来不错,但是提取 <head> ;你想移除头部。尝试使用 preg_replace 相反:

$without_head = preg_replace ('/(?:<head[^>]*>)(.*?)(<\/head>)/is', '', $contents);

关于php正则表达式问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7706731/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com