Skip to content

Instantly share code, notes, and snippets.

@ahmadnaser
Created June 23, 2014 10:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ahmadnaser/55918b6bd2cfefa6b151 to your computer and use it in GitHub Desktop.
Save ahmadnaser/55918b6bd2cfefa6b151 to your computer and use it in GitHub Desktop.
html regular expression in wordpress
Rating form has been successfully updated. <span class="rating-result "><span class="star-rating" style="color: #FDC709"><i class="fa fa-star"></i><i class="fa fa-star"></i><i class="fa fa-star"></i><i class="fa fa-star"></i><i class="fa fa-star-half-o"></i></span><span class="star-result">4.45/5</span></span>
/*Update Avg Rating On Update
* @Author :AhmadHammad
*
* Steps : 1-Update The Current Rating Indicators
* 2-Get The Html for the overall result
* 3-Parse it and get the rating from 5 . e.g 4.5
* */
ob_start();
MRP_Multi_Rating_API::display_rating_result(array(
'show_rich_snippets' => false,
'post_id' => $post_id,
'result_type' => 'star_rating',
'show_count' => false,
'echo' => true
));
$d=ob_get_clean();
@preg_match("/<span(?:[^>]+class=\"(star-result)\"[^>]*)?>(.*?)<\/span>/",$d, $output_array);
/*
*http://www.phpliveregex.com/
Array
(
[0] => <span class="star-result">4.2/5</span>
[1] => star-result
[2] => 4.2/5
)
*/
//print_r($output_array);
//split to get the first and second part 4.9/5 --> 4.9 , 5
list($bast, $makam) = split('[/]', $output_array[2]);
update_post_meta($post_id, 'rating_avg', $bast);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment