简体   繁体   English

使用PHP preg_match获取其他两个字符串之间的字符串

[英]Use PHP preg_match get a string between two other strings

I have the variable $Contents that contains the contents of a webpage and I need to pull out the following: 我有包含网页内容的变量$Contents ,我需要提取以下内容:

Start: <div class="XXXXX"> 开始: <div class="XXXXX">

End: <div role="XXXXX"' 结束: <div role="XXXXX"'

The string represented by YYYYY could be numbers, characters, spaces, quotes and pretty much anything that exists on a modern keyboard. YYYYY表示的字符串可以是数字,字符,空格,引号以及几乎任何现代键盘上存在的内容。

Currently I am using this: 目前,我正在使用此:

preg_match("/<div class=\"XXXXX\">(.*)<div role=\"XXXXX\"/", $Contents, $match);
echo "<p>Event Title: $match[1]</p>";

But getting nothing so I assume it's my regex that's the issue. 但是什么也没得到,所以我认为这是问题所在。 Can anyone help? 有人可以帮忙吗?

I'm assuming the second XXXXX should be YYYYY , or maybe you just mean it could be any string. 我假设第二个XXXXX应该是YYYYY ,或者也许您只是说它可以是任何字符串。

First, you really should use a parser instead of regex for this. 首先,您确实应该为此使用解析器而不是正则表达式。 See this classic, sad tale for the reason why. 请查看这个经典的悲惨故事的原因。

Second, to answer your question: add a ? 其次,回答您的问题:添加? after .* and use s after the final slash to match across lines, like this: .*之后,并在最终斜杠后使用s跨行匹配,如下所示:

$Contents = '<div class="XXXXX">
    foo bar
    <div role="alacadabra">baz';
preg_match("/<div class=\"XXXXX\">(.*)<div role=\".+\"/s", $Contents, $match);
echo "<p>Event Title: $match[1]</p>"; // outputs foo bar

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

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