Get all matches between 2 fixed texts

1

I am using regex to get all search results on a page:

<div class="col-xs-6 col-sm-4 col-md-4 col-lg-4">(NÚMERO ILIMITADO DE CARACTERES)</div>

At first I think I have it right, but it does not work for me.

$regex = '/\<div class\=\"col-xs-6 col-sm-4 col-md-4 col-lg-4\"\>(.*?)\<\/div\>/';

if(preg_match($regex, $html, $match)) {

    echo $match[0];

}

Can someone guide me?

    
asked by E.R.A. 17.12.2016 в 11:11
source

1 answer

1

preg_match returns only the first result.

int preg_match ( string $pattern , string $subject [, array &$matches [, int $flags = 0 [, int $offset = 0 ]]] )
     

Look in subject for a match to the regular expression given in pattern .

preg_match_all returns all the results.

int preg_match_all ( string $pattern , string $subject [, array &$matches [, int $flags = PREG_PATTERN_ORDER [, int $offset = 0 ]]] )
     

Search subject for all matches of the regular expression given in pattern and enter them in matches in the order specified by flags .

     

After having found the first match, subsequent searches will continue from the end of the match.

On the other hand, the period ( . ) matches all characters except line breaks, unless you specify the% modifier% co_ or SINGLELINE .

The following regular expression matches the text published in the question (there are no differences with the pattern of the question, I'm just not escaping the unnecessary characters, but it has no inference about the result):

/<div class="col-xs-6 col-sm-4 col-md-4 col-lg-4">(.*?)<\/div>/s

Demo: link

  

Important note :

     

However, to extract text from an HTML, should not be used   regular expressions . Instead, the correct way to do it is   using the DOM .

     

There are an infinity of examples that can be given that could make   certain expression fails in a given HTML (and the regex could be corrected,   but then another exception could be found). It does not have any   I felt reinventing the wheel in this case. For that, you have to use the   tools that were designed for that purpose.

    
answered by 17.12.2016 в 11:22