HiveBrain v1.2.0
Get Started
← Back to all entries
patternphpMinor

Improvement of a regular expression pattern

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
improvementexpressionregularpattern

Problem

I created this script to take informations "at-the-moment" of this site:

Pictures of name channel
Name of channel
Pictures channel
Times
Title
Type


My script (Work):

(.*?)/is', $code_page, $chaines);
preg_match_all('/(.*?)(.*?)/is', $code_page, $channels);

$i=0;
foreach ($channels as $channel) {
    if($i==3){
        for ($j=0; $j /', $channel[$j], $image[$j]); 
        }
    }

if($i==4){
        for ($j=0; $j (.*?)/', $channel[$j], $time[$j]);
            preg_match_all('/(.*?)/', $channel[$j], $title[$j]);
            preg_match_all('/(.*?)/', $channel[$j], $type[$j]);
        }
    }
    $i++;
}

for ($i=0; $i ');
    if($test != true){
        echo 'Chaines images: ';
        echo 'Chaines: '.$chaines[3][$i].'';
        echo 'image : '; 
        echo 'Temps : '.trim($time[$i][1][0]).'';
        echo 'Titre : '.substr($title[$i][1][0], strpos($title[$i][1][0], '>') + 1, strrpos($title[$i][1][0], '';
        echo 'Type : '.$type[$i][1][0].'';
        echo '';
    }
}
?>


I would like to improve and find a better regular expression pattern because I use 6 regular expression pattern.

Solution

To be honest I don't think your current way of handling is 'clean'.
But I would suggest you to use a HTML DOM parser. I took the liberty to google it for you and http://simplehtmldom.sourceforge.net/ seems quite what you need.

It's a PHP HTML parser, which allows you to write

// Find all element which class=foo
$ret = $html->find('.at-the-moment');


instead of

preg_match_all('/(.*?)(.*?)/is', $code_page, $channels);


Also your other regexes can be switched (read the documentation)

To be honest, I never used this library before, but I've worked with beautifulsoup which is a Python equivalent, which would do the trick for you perfectly.
(source: http://www.givegoodweb.com/post/210/html-parser-for-php)

Wishing you the best!

Code Snippets

// Find all element which class=foo
$ret = $html->find('.at-the-moment');
preg_match_all('/<div class="show (.*?) at-the-moment current (.*?)">(.*?)<div class="show-infos">(.*?)<\/div>/is', $code_page, $channels);

Context

StackExchange Code Review Q#74924, answer score: 5

Revisions (0)

No revisions yet.