0

I have to find a values between specific tags in HTML page through php regex. but I want if HTML page contain multiple value then do preg_match_all otherwise do nothing.

For example if preg_match find 4 values in HTML then do preg_match_all in next phase otherwise if it is preg_match find only 1 tag value then do nothing.

<td class"page"> 
<span class="my-tag">value1</span>
<span class="my-tag">value2</span>
<span class="my-tag">value3</span>
<span class="my-tag">value4</span>
</td>
preg_match('/<td class"page">(.*?)<\/td>/s';)
now do preg_match_all in next phase because preg_match find 4 values

preg_match_all('|\<span class="my-tag"\>(.*?)\</span\>|', $html, $string);

and if HTML contain only 1 value like this 
<td class"page"> 
<span class="my-tag">value1</span>
</td>

So if HTML contain only 1 value then do nothing

Andy Lester
  • 91,102
  • 13
  • 100
  • 152
smallbee
  • 233
  • 1
  • 4
  • 16
  • Unless you are dealing with a set of HTML files that will never change, parsing HTML with regular expressions is a road to sadness. As soon as the HTML changes from your expectations, your code will be broken. See http://htmlparsing.com/regexes for examples of why. **Use a proper HTML parsing module.** See [this SO thread](http://stackoverflow.com/questions/3577641/how-do-you-parse-and-process-html-xml-in-php) for examples of how to properly parse HTML with PHP modules that have already been written, tested and debugged. – Andy Lester Feb 23 '16 at 04:13

2 Answers2

0

Basically, from your preg_match, you would be getting a string back that looks like this:

Array
(
    [0] => <td class="page"> 
<span class="my-tag">value1</span>
<span class="my-tag">value2</span>
<span class="my-tag">value3</span>
<span class="my-tag">value4</span>
</td>
    [1] =>  
<span class="my-tag">value1</span>
<span class="my-tag">value2</span>
<span class="my-tag">value3</span>
<span class="my-tag">value4</span>

)

With that, we can just go ahead and do the match - regardless of if it only found one match or multiple matches. (Because it's not going to hurt anything to match one item or four items, I am proposing to move the logic down in your code.) Then we can just count how many it found and store that in a variable named $count.

// CHECK TO SEE IF WE FOUND A MATCH
if (isset($matches[1])) {

    // GO AHEAD AND DO THE MATCH ON THE SPANS
    preg_match_all('~<span class="my-tag">(.*?)</span>~s', $string, $span_matches);
    $count = count($span_matches[1]);


    // IF WE FOUND MULTIPLE MATCHES, LIST THEM OUT
    if ($count > 1) {
        print 'COUNT IS: '.$count;
        print_r($span_matches[1]);
    }
    // WE DID NOT MATCH ANY SPAN TAGS
    elseif ($count == 0) {
        print 'COUNT IS ZERO - CRAP';
    }
    // IF WE ONLY FOUND ONE MATCH, WE DON'T NEED TO DO ANYTHING
    else {
        print 'COUNT IS EXACTLY 1 - DO NOTHING';
    }

}
// WE DID NOT FIND AN INITAL MATCH TO BEGIN WITH
else {
    print 'WE DID NOT FIND A MATCH';
}

From there, it's just a simple if/else statement to do what you want with it.

Here is a working demo:

http://ideone.com/SiPiOx

Quixrick
  • 3,190
  • 1
  • 14
  • 17
0

You can combine multiple patterns in a single regex with the pipe character in parentheses:

preg_match('/(cats?|dogs?|re.*tion)/', $string, $matches);
DaveL17
  • 1,673
  • 7
  • 24
  • 38
Abdul Rehman
  • 142
  • 1
  • 12