';
}
function bx_linkify($text, $sAttrs = '', $bHtmlSpecialChars = false)
{
if ($bHtmlSpecialChars)
$text = htmlspecialchars($text, ENT_NOQUOTES, 'UTF-8');
$re = "@\b((https?://)|(www\.))(([0-9a-zA-Z_!~*'().&=+$%-]+:)?[0-9a-zA-Z_!~*'().&=+$%-]+\@)?(([0-9]{1,3}\.){3}[0-9]{1,3}|([0-9a-zA-Z_!~*'()-]+\.)*([0-9a-zA-Z][0-9a-zA-Z-]{0,61})?[0-9a-zA-Z]\.[a-zA-Z]{2,6})(:[0-9]{1,4})?((/[0-9a-zA-Z_!~*'().;?:\@&=+$,%#-]+)*/?)@";
preg_match_all($re, $text, $matches, PREG_OFFSET_CAPTURE);
$matches = $matches[0];
$i = count($matches);
while ($i--)
{
$url = $matches[$i][0];
if (!preg_match('@^https?://@', $url))
$url = 'http://'.$url;
$text = substr_replace($text, ''.$matches[$i][0].'', $matches[$i][1], strlen($matches[$i][0]));
}
return $text;
}
/**
* Wrap in A tag links in HTML string, which aren't wrapped in A tag yet
* @param $sHtmlOrig - HTML string
* @param $sAttrs - attributes string to add to the added A tag
* @return modified HTML string, in case of errror original string is returned
*/
function bx_linkify_html($sHtmlOrig, $sAttrs = '')
{
if (!trim($sHtmlOrig))
return $sHtmlOrig;
$sId = 'bx-linkify-' . md5(microtime());
$dom = new DOMDocument();
@$dom->loadHTML('
' . $sHtmlOrig . '
');
$xpath = new DOMXpath($dom);
foreach ($xpath->query('//text()') as $text) {
$frag = $dom->createDocumentFragment();
$frag->appendXML($this->bx_linkify($text->nodeValue, $sAttrs, true));
$text->parentNode->replaceChild($frag, $text);
}
if (version_compare(PHP_VERSION, '5.3.6') >= 0)
$s = $dom->saveHTML($dom->getElementById($sId));
else
$s = $dom->saveXML($dom->getElementById($sId), LIBXML_NOEMPTYTAG);
if (false === $s) // in case of error return original string
return $sHtmlOrig;
if (false !== ($iPos = mb_strpos($s, '')) && $iPos < mb_strpos($s, $sId))
$s = mb_substr($s, $iPos + 12, -15); // strip tags and everything before them
return mb_substr($s, 54, -6); // strip added tags
}
//END HACK
}