在div中查找所有内容

在div中查找所有内容

本文介绍了regex php:在div中查找所有内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用regexp在div内查找eveything.我知道可能有一种更聪明的方法来执行此操作-但我选择了regexp.

I'm trying to find eveything inside a div using regexp. I'm aware that there probably is a smarter way to do this - but I've chosen regexp.

所以目前我的正则表达式模式如下:

so currently my regexp pattern looks like this:

$gallery_pattern = '/<div class="gallery">([\s\S]*)<\/div>/';

它可以解决问题-

问题是如果我彼此之间有两个div,就像这样.

The problem is if i have two divs after each other - like this.

<div class="gallery">text to extract here</div>
<div class="gallery">text to extract from here as well</div>

我想从两个div中提取信息,但是在测试时,我的问题是我没有得到中间的文本,而是:

I want to extract the information from both divs, but my problem, when testing, is that im not getting the text in between as a result but instead:

"text to extract here </div>
<div class="gallery">text to extract from here as well"

所以总结一下.它跳过div的第一端.并继续进行下一个.div中的文本可以包含</和换行符.只是让您知道!

So to sum up. It skips the first end of the div. and continues on to the next.The text inside the div can contain <, / and linebreaks. just so you know!

有人对这个问题有简单的解决方案吗?我仍然是正则表达式新手.

Does anyone have a simple solution to this problem? Im still a regexp novice.

推荐答案

这样的事情怎么样:

$str = <<<HTML
<div class="gallery">text to extract here</div>
<div class="gallery">text to extract from here as well</div>
HTML;

$matches = array();
preg_match_all('#<div[^>]*>(.*?)</div>#', $str, $matches);

var_dump($matches[1]);

注意?"在正则表达式中,因此它不是贪婪的".

Note the '?' in the regex, so it is "not greedy".

哪位可以帮助您:

array
  0 => string 'text to extract here' (length=20)
  1 => string 'text to extract from here as well' (length=33)

这应该可以正常工作...如果您没有divs的话;如果您这样做了……那么……实际上:您真的确定要使用有理表达式来解析HTML,这本身并不是那个有理吗?

This should work fine... If you don't have imbricated divs ; if you do... Well... actually : are you really sure you want to use rational expressions to parse HTML, which is quite not that rational itself ?

这篇关于regex php:在div中查找所有内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-03 19:49