Created
March 11, 2011 20:03
-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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