diff --git a/.DS_Store b/.DS_Store new file mode 100644 index 0000000..51c476f Binary files /dev/null and b/.DS_Store differ diff --git a/.phpintel/052b22ff0e04838a4cab006c50e58961 b/.phpintel/052b22ff0e04838a4cab006c50e58961 new file mode 100644 index 0000000..0989a66 Binary files /dev/null and b/.phpintel/052b22ff0e04838a4cab006c50e58961 differ diff --git a/.phpintel/0c23ba91e4716b6f699f904ac8e649cc b/.phpintel/0c23ba91e4716b6f699f904ac8e649cc new file mode 100644 index 0000000..798ef52 Binary files /dev/null and b/.phpintel/0c23ba91e4716b6f699f904ac8e649cc differ diff --git a/.phpintel/index b/.phpintel/index new file mode 100644 index 0000000..218cd67 Binary files /dev/null and b/.phpintel/index differ diff --git a/src/.DS_Store b/src/.DS_Store new file mode 100644 index 0000000..e91803b Binary files /dev/null and b/src/.DS_Store differ diff --git a/src/Misd/.DS_Store b/src/Misd/.DS_Store new file mode 100644 index 0000000..24071bf Binary files /dev/null and b/src/Misd/.DS_Store differ diff --git a/src/Misd/Linkify/Linkify.php b/src/Misd/Linkify/Linkify.php index 0e3c9e6..b197b11 100644 --- a/src/Misd/Linkify/Linkify.php +++ b/src/Misd/Linkify/Linkify.php @@ -13,6 +13,8 @@ /** * Converts URLs and/or email addresses into HTML links. + * + * @author Chris Wilkinson */ class Linkify implements LinkifyInterface { @@ -38,7 +40,7 @@ public function __construct(array $options = array()) */ public function process($text, array $options = array()) { - return $this->linkify($text, true, true, $options); + return $this->linkify($text, true, false, true, $options); } /** @@ -46,7 +48,7 @@ public function process($text, array $options = array()) */ public function processUrls($text, array $options = array()) { - return $this->linkify($text, true, false, $options); + return $this->linkify($text, true, false, false, $options); } /** @@ -54,9 +56,13 @@ public function processUrls($text, array $options = array()) */ public function processEmails($text, array $options = array()) { - return $this->linkify($text, false, true, $options); - } + return $this->linkify($text, false, true, false, $options); + } + public function processTwitterHandles($text, array $options = array()) + { + return $this->linkify($text, false, false, true, $options); + } /** * Add links to text. * @@ -67,9 +73,9 @@ public function processEmails($text, array $options = array()) * * @return string Linkified text. */ - protected function linkify($text, $urls = true, $emails = true, array $options = array()) + protected function linkify($text, $urls = true, $emails = true, $twitter = true, array $options = array()) { - if (false === $urls && false === $emails) { + if (false === $urls && false === $emails && false === $twitter) { // nothing to do... return $text; } @@ -105,6 +111,9 @@ protected function linkify($text, $urls = true, $emails = true, array $options = if (true === $emails) { $chunks[$i] = $this->linkifyEmails($chunks[$i], $options); } + if (true === $twitter) { + $chunks[$i] = $this->linkifyTwitter($chunks[$i], $options); + } } } else { // odd numbers are tags // Only process this tag if there are no unclosed $ignoreTags @@ -174,7 +183,7 @@ protected function linkifyUrls($text, $options = array('attr' => '')) } } - return '' . $caption . ''; + return '' . $caption . ''; }; return preg_replace_callback($pattern, $callback, $text); @@ -208,9 +217,39 @@ protected function linkifyEmails($text, $options = array('attr' => '')) } } - return '' . $match[0] . ''; + return '' . $match[0] . ''; + }; + + return preg_replace_callback($pattern, $callback, $text); + } + + /** + * Add HTML links to Twitter Handles in plain text. + * + * @param string $text Text to linkify. + * @param array $options Options, 'attr' key being the attributes to add to the links, with a preceding space. + * + * @return string Linkified text. + */ + + protected function linkifyTwitter($text, $options = array('attr' => '')) + { + $pattern = '/\B@[^\b]([^.\s]+)/'; + //$pattern = '/@([\w]+)([[^\s])/i'; + ///$pattern = '/@([\w]+)([^\s]+)/i'; + + $callback = function ($match) use ($options) { + if (isset($options['callback'])) { + $cb = $options['callback']($match[0], $match[0], true); + if (!is_null($cb)) { + return $cb; + } + } + + return ''. $match[0] .''; }; return preg_replace_callback($pattern, $callback, $text); } + } diff --git a/src/Misd/Linkify/LinkifyInterface.php b/src/Misd/Linkify/LinkifyInterface.php index 7b589ef..6fb8bda 100644 --- a/src/Misd/Linkify/LinkifyInterface.php +++ b/src/Misd/Linkify/LinkifyInterface.php @@ -13,6 +13,8 @@ /** * Converts URLs and/or email addresses into HTML links. + * + * @author Chris Wilkinson */ interface LinkifyInterface { @@ -45,4 +47,14 @@ public function processUrls($text, array $options = array()); * @return string Processed text. */ public function processEmails($text, array $options = array()); + + /** + * Add HTML links to @Twitter Handles + * + * @param string $text Text to process. + * @param array $options Options. + * + * @return string Processed text. + */ + public function processTwitterHandles($text, array $options = array()); }