Search code examples
phphtmlpreg-match-all

RegEx for matching a HTML string


I'm trying to do a preg_match_all, which does not work.

preg_match_all(
    '!<div class="Description_Productinfo" itemprop="description"><p><span style="color:#7f8c8d;"><span style="font-family:Arial,Helvetica,sans-serif;"><span style="font-size:14px;">(.*?)</span></span></span></p></div>!',
    $html,
    $matches1,
    PREG_SET_ORDER
);
foreach ($matches1 as $soge2){print_r($soge2);}

How do I make it work?


Solution

  • It may not be the best idea to parse HTMLs with RegEx, which seems that you may want to do. However, based on your question, I'm guessing that you may wish to get all data from this tag using (.*):

    <span style="font-size:14px;"></span>
    

    It is possible to do so. First, you may design your preg_match_all(); based on:

    preg_match_all($pattern, $subject, $matches);
    

    which in this case, your subject is your HTML input, and I'm not sure, but your pattern might be:

    /<span style=\"font-size:14px;\">([\w\s]+)(<\/span>)/s 
    

    Code

    $html = '<div class="Description_Productinfo" itemprop="description"><p><span style="color:#7f8c8d;"><span style="font-family:Arial,Helvetica,sans-serif;"><span style="font-size:14px;">Alphanumeric Words If You Wish To Match</span></span></span></p></div>';
    
    preg_match_all('/<span style=\"font-size:14px;\">([\w\s]+)(<\/span>)/s', $html, $matches1, PREG_SET_ORDER);
    var_dump($matches1);
    
    foreach ($matches1 as $soge2) {
        print_r($soge2);
    }
    

    Output

    Array
    (
        [0] => <span style="font-size:14px;">Alphanumeric Words If You Wish To Match</span>
        [1] => Alphanumeric Words If You Wish To Match
        [2] => </span>
    )
    

    Pattern

    You may use a tool to design your desired pattern, such as:

    enter image description here