Skip to content

Commit 78e3288

Browse files
author
e1himself
committed
Rewrite complex TextHelper create_function code to a closure
1 parent 2303d1f commit 78e3288

File tree

1 file changed

+13
-22
lines changed

1 file changed

+13
-22
lines changed

lib/helper/TextHelper.php

Lines changed: 13 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -278,33 +278,24 @@ function _auto_link_urls($text, $href_options = array(), $truncate = false, $tru
278278
{
279279
$href_options = _tag_options($href_options);
280280

281-
$callback_function = '
282-
if (preg_match("/<a\s/i", $matches[1]))
283-
{
284-
return $matches[0];
285-
}
286-
';
287-
288-
if ($truncate)
289-
{
290-
$callback_function .= '
291-
else if (strlen($matches[2].$matches[3]) > '.$truncate_len.')
281+
$callback_function = function($matches) use ($href_options, $truncate, $truncate_len, $pad) {
282+
if (preg_match("/<a\s/i", $matches[1]))
292283
{
293-
return $matches[1].\'<a href="\'.($matches[2] == "www." ? "http://www." : $matches[2]).$matches[3].\'"'.$href_options.'>\'.substr($matches[2].$matches[3], 0, '.$truncate_len.').\''.$pad.'</a>\'.$matches[4];
284+
return $matches[0];
294285
}
295-
';
296-
}
297-
298-
$callback_function .= '
299-
else
300-
{
301-
return $matches[1].\'<a href="\'.($matches[2] == "www." ? "http://www." : $matches[2]).$matches[3].\'"'.$href_options.'>\'.$matches[2].$matches[3].\'</a>\'.$matches[4];
302-
}
303-
';
286+
else if ($truncate && strlen($matches[2].$matches[3]) > $truncate_len)
287+
{
288+
return $matches[1].'<a href="'.($matches[2] == "www." ? "http://www." : $matches[2]).$matches[3].'"'.$href_options.'>'.substr($matches[2].$matches[3], 0, $truncate_len).$pad.'</a>'.$matches[4];
289+
}
290+
else
291+
{
292+
return $matches[1].'<a href="'.($matches[2] == "www." ? "http://www." : $matches[2]).$matches[3].'"'.$href_options.'>'.$matches[2].$matches[3].'</a>'.$matches[4];
293+
}
294+
};
304295

305296
return preg_replace_callback(
306297
SF_AUTO_LINK_RE,
307-
create_function('$matches', $callback_function),
298+
$callback_function,
308299
$text
309300
);
310301
}

0 commit comments

Comments
 (0)