Skip to content

Commit f2ebd7d

Browse files
author
e1himself
committed
Refactor TextHelper _auto_link_urls logic to improve code readability
1 parent 78e3288 commit f2ebd7d

File tree

1 file changed

+8
-6
lines changed

1 file changed

+8
-6
lines changed

lib/helper/TextHelper.php

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -283,14 +283,16 @@ function _auto_link_urls($text, $href_options = array(), $truncate = false, $tru
283283
{
284284
return $matches[0];
285285
}
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
286+
287+
$text = $matches[2] . $matches[3];
288+
$href = ($matches[2] == "www." ? "http://www." : $matches[2]) . $matches[3];
289+
290+
if ($truncate && strlen($text) > $truncate_len)
291291
{
292-
return $matches[1].'<a href="'.($matches[2] == "www." ? "http://www." : $matches[2]).$matches[3].'"'.$href_options.'>'.$matches[2].$matches[3].'</a>'.$matches[4];
292+
$text = substr($text, 0, $truncate_len).$pad;
293293
}
294+
295+
return sprintf('%s<a href="%s"%s>%s</a>%s', $matches[1], $href, $href_options, $text, $matches[4]);
294296
};
295297

296298
return preg_replace_callback(

0 commit comments

Comments
 (0)