Skip to content
Browse files

MDL-21817 replace use of preg_match() with /e modifier with preg_repl…

…ace_callback() and preg_match_all() with a loop
  • Loading branch information...
1 parent b45b9ea commit 9f23ea03b1efc0a170c00d4e2834f63255189724 @gbateson gbateson committed Mar 15, 2010
Showing with 21 additions and 5 deletions.
  1. +21 −5 question/format/hotpot/format.php
View
26 question/format/hotpot/format.php
@@ -541,10 +541,16 @@ function hotpot_get_reading(&$xml) {
}
function hotpot_prepare_str($str) {
// convert html entities to unicode and add slashes
- $str = preg_replace('/&#x([0-9a-f]+);/ie', "hotpot_charcode_to_utf8(hexdec('\\1'))", $str);
- $str = preg_replace('/&#([0-9]+);/e', "hotpot_charcode_to_utf8(\\1)", $str);
+ $str = preg_replace_callback('/&#([0-9]+);/', array(&$this, 'hotpot_prepare_str_dec'), $str);
+ $str = preg_replace_callback('/&#x([0-9a-f]+);/i', array(&$this, 'hotpot_prepare_str_hexdec'), $str);
return addslashes($str);
}
+ function hotpot_prepare_str_dec(&$matches) {
+ return hotpot_charcode_to_utf8($matches[1]);
+ }
+ function hotpot_prepare_str_hexdec(&$matches) {
+ return hotpot_charcode_to_utf8(hexdec($matches[1]));
+ }
} // end class
function hotpot_charcode_to_utf8($charcode) {
@@ -588,9 +594,19 @@ function hotpot_convert_relative_urls($str, $baseurl, $filename) {
} else {
$url = '.*?';
}
- $search = "%($tagopen$tag$space$anychar$attribute=$quoteopen)($url)($quoteclose$anychar$tagclose)%ise";
- $str = preg_replace($search, $replace, $str);
+ $search = "/($tagopen$tag$space$anychar$attribute=$quoteopen)($url)($quoteclose$anychar$tagclose)/is";
+ if (preg_match_all($search, $str, $matches, PREG_OFFSET_CAPTURE)) {
+ $i_max = count($matches[0]) - 1;
+ for ($i=$i_max; $i>=0; $i--) {
+ $match = $matches[0][$i][0];
+ $start = $matches[0][$i][1];
+ $replace = hotpot_convert_relative_url(
+ $baseurl, $filename, $matches[1][$i][0], $matches[6][$i][0], $matches[7][$i][0], false
+ );
+ $str = substr_replace($str, $replace, $start, strlen($match));
+ }
+ }
}
return $str;
-}
+}

0 comments on commit 9f23ea0

Please sign in to comment.
Something went wrong with that request. Please try again.