Как получить данные из определенного div с preg_match? - PullRequest
0 голосов
/ 12 января 2012
Example: <div style="font-size:15px;">first matched here</div>

Я хочу, чтобы PHP нашел информацию в этом конкретном DIV

Я перепробовал много функций preg_match, но не понимаю REGEX.

HELP

Ответы [ 2 ]

1 голос
/ 12 января 2012
<?php
$data = 'aaa <div class="main">content</div> bbb';
preg_match_all ("/<div.*?>([^`]*?)<\/div>/", $data, $matches);
//testing the array $matches
//$matches is an array contains all the matched div elements and its contents
//print_r($matches);
?>

Попробуйте это для сопоставления всех элементов div

1 голос
/ 12 января 2012

попробуйте это:

<code><?php
    //the string to search in
    $s = "<div style=\"font-size:15px;\">first matched here</div>";
    //set up an empty array to facilitate the results
    $matches = array();
    //match with regex
    //pattern has to start and end with the same character (of your choice)
    //here it's #
    //start criteria is an opening div <div> that may contain some text
    //between v and >, so use .*?
    //the dot means "any character", the *? means "any number, but as few as possible"
    //then, define the part to capture. this is done with paranthesis
    //define what to capture. this would be any character, and as few as possible
    //and finally end criteria </div>
    //matches are stored in the out parameter $matches
    preg_match("#<div.*?>(.*?)</div>#", $s, $matches);
    echo "<pre>";
    print_r($matches);
    echo "
";
...