[英]How do I match between two closing tags with regex in php?
I have a piece of HTML ( NOT a full HTML document) that has a few instances of the following: 我有一段HTML( 不是完整的HTML文档),其中包含以下示例:
</div>
some text is here
</h3>
I'd like to match 'some text is here' only but am having trouble understanding multiple lines, line breaks and spaces with php regex. 我只想匹配“一些文本在这里”,但是用php regex难以理解多行,换行和空格。 What I got is:
我得到的是:
preg_match('/<\/div>[\s\r\n\t]*(.*)[\s\n\r\t]*<\/h3>/', $string, $matches);
But that doesn't seem to work. 但这似乎不起作用。 I tried using DOMDocument() as well but it's throwing all sorts of errors, probably because this is not a full HTML document.
我也尝试使用DOMDocument(),但是它引发了各种错误,可能是因为这不是完整的HTML文档。
Any ideas?? 有任何想法吗??
Use something like 使用类似
preg_match("@</div>(.*?)</h3>@s", $html, $matches);
The s
PCRE modifier (after the last @
) allows the .
s
PCRE修饰符(在最后一个@
)允许使用.
to match newlines. 匹配换行符。 The
.*?
.*?
is done so that it will only match up to the first </h3>
instead of the last. 这样做的话,它只会匹配第一个
</h3>
而不是最后一个。 The data between the tags will be in $matches[1]
标签之间的数据将在
$matches[1]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.