RegExp PHP 获取多个 span 标签之间的文本
我英语说得不太好。所以,如果我犯了一些错误,请抱歉。
在网站上,我有一个 div 框,其中包含有关游戏的一些信息:
<span class="noteline">Developer:</span>
<span class="subline">Gameloft</span>
<span class="noteline">Genre:</span>
<span class="subline">Racing/Arcade</span>
<span class="noteline">Release year:</span>
<span class="subline">2010</span>
我需要获取 和它的结束标记
之间的信息。 上面的
preg_match("/\<span\sclass=\"subline\"\>(.*)<\/span\>/imsU", $source, $matches);
解决方案工作正常,但它只获得带有文本“gameloft”的“子行”;
但我还需要包含文本 Racing/Arcade 和 2010 的子行;
也许是这样的(这不起作用);
for developer = preg_match("/*(\<span\sclass=\"subline\"\>){1}*(.*)*(<\/span\>){1}*/imsU", $source, $matches);
for genre = preg_match("/*(\<span\sclass=\"subline\"\>){2}*(.*)*(<\/span\>){2}*/imsU", $source, $matches);
像这样的东西..
无论如何。感谢您的任何帮助。
I don't speak English very well. So, if i'll make some mistake please sorry.
On the site i have a div box with some information about game:
<span class="noteline">Developer:</span>
<span class="subline">Gameloft</span>
<span class="noteline">Genre:</span>
<span class="subline">Racing/Arcade</span>
<span class="noteline">Release year:</span>
<span class="subline">2010</span>
I need to get the information between <span class="noteline">
and it's closing tag </span>
preg_match("/\<span\sclass=\"subline\"\>(.*)<\/span\>/imsU", $source, $matches);
the solution above works fine but it only gets the "subline" with text "gameloft";
but i need also sublines that have text Racing/Arcade and 2010;
Maybe something like this (that doesn't work);
for developer = preg_match("/*(\<span\sclass=\"subline\"\>){1}*(.*)*(<\/span\>){1}*/imsU", $source, $matches);
for genre = preg_match("/*(\<span\sclass=\"subline\"\>){2}*(.*)*(<\/span\>){2}*/imsU", $source, $matches);
something like this..
Anyway. Thanks for any help.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
正则表达式的替代方法是使用 phpQuery 或 QueryPath,这将其简化为:
An alternative to regexps would be to use phpQuery or QueryPath, which simplifies it to:
正则表达式不适合解析 HTML。它们很难正确执行,并且总是在边缘情况下崩溃。
我不知道是否有更简单的方法,但这应该适用于您描述的标记:
这假设
class="subline"
所以它会因多个类而失败。 (Xpath 的新手,欢迎改进。)Regular expressions are not appropriate to parse HTML. They are difficult to get right and they always break in edge cases.
I don't know if there's an easier way but this should work with the markup you describe:
This assumes
class="subline"
so it'll fail with multiple classes. (New to Xpath so improvements welcome.)试试这个:
我这样尝试了上面的代码:
我得到的输出是这样的:
Try this:
I tried the above code this way:
The output I got was this: