Skip to content

Instantly share code, notes, and snippets.

@lemon-tree
Created March 11, 2011 20:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save lemon-tree/866475 to your computer and use it in GitHub Desktop.
Save lemon-tree/866475 to your computer and use it in GitHub Desktop.
Replaces non-anchor wrapped urls with anchors, also filters attributes on current anchors. This is a Gist so that if you have an issue you can
<?php
$string = 'Lorem ipsum dolor sit amet, consectetur http://google.com</a> adipiscing elit. http://google.com/ Pellentesque vel lectus nec arcu malesuada eleifend eget non lectus. Nullam id tortor velit. Pellentesque ac nunc dui. Nunc dapibus, metus ac ullamcorper <a href="http://google.com" onclick="fff d">luctus</a>, arcu dolor pulvinar massa, ut gravida justo nulla a risus. Integer sit amet urna ut mauris scelerisque sollicitudin. Nulla fringilla ligula nec mi vehicula in hendrerit libero volutpat. <a href="http://google.com">http://google.com</a>';
class LinkLocator {
//If you change this regex, change the duplicate bit in the main regex. I should really be doing this progmatically
protected static $attrRegex = '(?:\S+?=(?:(?:\'.*?\')|(?:".*?")\s*))';
protected static $replaceRegex = '/(?<anchor><a(?:\s+(?<attr>(?:\S+?=(?:(?:\'.*?\')|(?:".*?")\s*))+))?>(?<text>.*?)<\/a\s*>)|(?<!>)(?<url>(?<proto>https?:\/{2})(?<domain>[a-zA-Z0-9\-.]+\.[a-zA-Z]{2,3})(?<path>\/\S*)?)/i';
protected static $filterAttributes = true;
protected static $allowedAttributes = array('href');
public static function parseString ($parseString) {
//use an anonymous function if you desire, but it will only work in PHP > 5.3
return preg_replace_callback(self::$replaceRegex, array('self', 'linkCallback'), $parseString);
}
protected static function linkCallback($matches) {
if (strlen($matches['anchor']) > 0) {
if (self::$filterAttributes) {
$attributeString = '';
if (strlen($matches['attr']) > 0) {
preg_match_all('/' . self::$attrRegex . '/i', $matches['attr'], $attributes);
foreach ($attributes[0] as $attribute) {
$attibuteSplit = explode('=', $attribute);
if (in_array($attibuteSplit[0], self::$allowedAttributes)) {
$attributeString .= ' ' . $attribute;
}
}
}
} else {
$attributeString = ' ' . $matches['attr'];
}
return '<a' . $attributeString . '>' . $matches['text'] . '</a>';
} else {
$url = $matches['proto'] . $matches['domain'] . $matches['path'];
return '<a href="' . $url . '">' . $url . '</a>';
}
}
}
echo LinkLocator::parseString($string);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment