summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorCraig Andrews <candrews@integralblue.com>2009-08-21 12:47:01 -0400
committerCraig Andrews <candrews@integralblue.com>2009-08-21 12:47:01 -0400
commit70ca03f33615f0d6504df91cd40fa04a228fa42f (patch)
tree7972ec52ebc9ec871c9a2a64ba34f45d33d7810e
parenta8d35451823b01c074d5417a138d4968331a8130 (diff)
Use currying to call the url callbacks, and use preg_replace_callback
This definitely looks neater than the string maniplation it replaces
-rw-r--r--lib/util.php38
1 files changed, 23 insertions, 15 deletions
diff --git a/lib/util.php b/lib/util.php
index 748c8332f..f732d8b55 100644
--- a/lib/util.php
+++ b/lib/util.php
@@ -445,24 +445,32 @@ function common_replace_urls_callback($text, $callback, $notice_id = null) {
'(?:[:/][^\s]*)?'.
')'.
'#ix';
- preg_match_all($regex, $text, $matches);
- // Then clean up what the regex left behind
- $offset = 0;
- foreach($matches[1] as $url) {
- // Call user specified func
- if (empty($notice_id)) {
- $modified_url = call_user_func($callback, $url);
- } else {
- $modified_url = call_user_func($callback, array($url, $notice_id));
- }
- // Replace it!
- $start = mb_strpos($text, $url, $offset);
- $text = mb_substr($text, 0, $start).$modified_url.mb_substr($text, $start + mb_strlen($url), mb_strlen($text));
- $offset = $start + mb_strlen($modified_url);
+ $callback_helper = curry(callback_helper, 3);
+ return preg_replace_callback($regex, $callback_helper($callback,$notice_id) ,$text);
+}
+
+function callback_helper($callback, $notice_id, $matches) {
+ if(empty($notice_id)){
+ return $callback($matches[1],$notice_id);
+ }else{
+ return $callback($matches[1]);
}
+}
- return $text;
+function curry($func, $arity) {
+ return create_function('', "
+ \$args = func_get_args();
+ if(count(\$args) >= $arity)
+ return call_user_func_array('$func', \$args);
+ \$args = var_export(\$args, 1);
+ return create_function('','
+ \$a = func_get_args();
+ \$z = ' . \$args . ';
+ \$a = array_merge(\$z,\$a);
+ return call_user_func_array(\'$func\', \$a);
+ ');
+ ");
}
function common_linkify($url) {