Exemple #1
0
 public static function format($str, $options = array())
 {
     // it need to be escaped first
     $str = StreamTemplate::escape($str);
     // Value return from getMentionUserId
     // array( $userId => array('@username', 'username'));
     $mentionedUserId = self::getMentionUserId($str);
     // Check options for required processing
     $uriBase = '';
     if (isset($options['fullUri']) && $options['fullUri']) {
         $uri = JURI::getInstance();
         $uriBase = $uri->toString(array('scheme', 'host', 'port'));
     }
     // Replacer will replace the url only after autolink perform
     // to avoid autolink automatically linking on <a> tag urls again
     $replacer = array();
     foreach ($mentionedUserId as $userid => $username) {
         $userid = $username[3];
         if (!empty($userid)) {
             $user = JXFactory::getUser($userid);
             $uid = '%username.replace.' . $userid . '%';
             $replacer[$uid] = $uriBase . JRoute::_('index.php?option=com_profile&view=display&user='******'<a href="' . $uid . '">' . StreamTemplate::escape($user->name) . '</a>', $str);
         }
     }
     // !important: We need to autolink before adding hash tag as regex hashtag will disturb tag inside a url
     // example: http://lightglitch.github.com/bootstrap-xtra/#grid-system
     // character #grid-system will be add as a tag and break the url
     // make sure the url is safely converted via StreamMessage::autoLink() and then grab the hashtag
     $str = StreamMessage::autoLink($str);
     // Autolinked #hashtag
     preg_match_all("" . "/[\n\\s#]#(?!\\.)([^\\s\",']+[^\n.\\s])|^#([^\\s]+)/" . "", $str, $matches, PREG_SET_ORDER);
     if (!empty($matches)) {
         foreach ($matches as $hashtag) {
             // match might be inside second capture and no deeper
             $hashtag1 = !empty($hashtag[1]) ? $hashtag[1] : $hashtag[2];
             $uid = '%hashtag.replace.' . $hashtag1 . '%';
             $replacer[$uid] = $uriBase . JRoute::_('index.php?option=com_stream&view=company&task=tagFilter&search=HASHTAG' . urlencode($hashtag1));
             $str = str_ireplace($hashtag[0], '<a class="tag" href="' . $uid . '">' . $hashtag1 . '</a>', $str);
         }
     }
     // Nl2br , convert multiple lines breaks to just 1
     $str = str_ireplace(array("\r\n", "\r", "\n"), "<br />", $str);
     // Ps:// i split the ? and > to assist with editor syntax highlighting
     $str = preg_replace("/(<br\\s*\\/?" . ">\\s*){3,}/", "<br /><br />", $str);
     // Replace back <a> tags
     foreach ($replacer as $replaceNeedle => $replaceVal) {
         $str = str_replace($replaceNeedle, $replaceVal, $str);
     }
     // Hashtag repplacer
     $str = str_replace('HASHTAG', '%23', $str);
     // if the tag is started and generated like so ##tag, the url will be precede with
     // another # symbol normalize this behaviour
     $str = str_replace('%23#', '%23', $str);
     return $str;
 }