2012-01-12 6 views

答えて

1
<?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); 
?> 

+1

なぜ**(?[^ '] *)**すべてのdiv要素を一致させるためにこれを試してみてください? – davogotland

1

これを試してみる:

<?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 "</pre>"; 
関連する問題