How should I modify this php preg_match now that I added an extra div around the span tags?
I used to apply this php preg_match:
<?php preg_match( '!<div class="thumblock ">(.*)</div>!si' , wp_gdsr_render_article_thumbs(0, false, "", 0, "", false) , $n );
$thumbs_number = strip_tags( $n[1] ); ?>
to extract the number (in this case 2) between the .rating-res开发者_运维技巧ult
span tags (only):
<div class="thumblock">
<span class="rating-result">2</span>
<div class="ratingtext">
<div id="gdsr_thumb_124_a_up" class="gdt-size-20 gdthumb gdup">
<div class="gdt-starrating"></div>
</div>
<div id="gdsr_thumb_124_a_dw" class="gdt-size-20 gdthumb gddw">
<div class="gdt-starrating"></div>
</div>
</div>
<div class="raterclear"></div>
</div>
Now, I modified the output (I added an extra div which surrounds the .rating-result
span tags):
<div class="thumblock">
<div id="gdsr_thumb_text_137_a" class="gdt-size-20 voted inactive gdthumbtext">
<span class="rating-result">2</span>
</div>
<div class="ratingtext">
<div id="gdsr_thumb_137_a_up" class="gdt-size-20 gdthumb gdup">
<div class="gdt-starrating"></div>
</div>
<div id="gdsr_thumb_137_a_dw" class="gdt-size-20 gdthumb gddw">
<div class="gdt-starrating"></div>
</div>
</div>
<div class="raterclear"></div>
</div>
Doing var_dump
to the first $thumbs_number
outputs: string(2) "+1"
Doing var_dump
to the second $thumbs_number
outputs: string(2) "+1"
But the function where I use the variable $thumbs_number
doesn't work any more (get_rating_class();
is placed in a div's class to add a class according to the number of thumbs):
function get_rating_class($thumbs_number) {
if ($thumbs_number < 0) return ' bad';
if ($thumbs_number < 2) return ' average';
if ($thumbs_number < 4) return ' good';
return ' excellent';
}
function rating_class($thumbs_number) {
echo get_rating_class($thumbs_number);
}
Which is absolutely the same, but it seems like
How should I modified the preg_match
in order to make it extract that number as it did before?
Your pattern:
'!<div class="thumblock ">(.*)</div>!si'
has a space after thumblock. Once I removed that, it worked ok for me. Note however that the (.) part of the pattern may include extra divs other than the thumblock one, as it matches greedily. If you make it match non-greedily by changing it to (.?), then it will only match to the first it finds, which may not be the thumblock div.
These problems occur because of the limitations of using regular expressions - they're not designed to parse nested tags, such as html. The answer to this question might help: preg_match() find all values inside of table?
精彩评论