replaceButProtectCodeBlocks() public static method

The three parameters are identical to the ones you'd pass preg_replace.
public static replaceButProtectCodeBlocks ( mixed $Search, mixed $Replace, mixed $Subject, boolean $IsCallback = false ) : string
$Search mixed The value being searched for, just like in preg_replace or preg_replace_callback.
$Replace mixed The replacement value, just like in preg_replace or preg_replace_callback.
$Subject mixed The string being searched.
$IsCallback boolean If true, do preg_replace_callback. Do preg_replace otherwise.
return string
Ejemplo n.º 1
0
 public static function mentions($Mixed)
 {
     if (!is_string($Mixed)) {
         return self::to($Mixed, 'Mentions');
     } else {
         // Check for a custom formatter.
         $Formatter = Gdn::factory('MentionsFormatter');
         if (is_object($Formatter)) {
             return $Formatter->formatMentions($Mixed);
         }
         // Handle @mentions.
         if (C('Garden.Format.Mentions')) {
             $urlFormat = str_replace('{name}', '$2', self::$MentionsUrlFormat);
             // Unicode includes Numbers, Letters, Marks, & Connector punctuation.
             $Pattern = unicodeRegexSupport() ? '[\\pN\\pL\\pM\\pPc]' : '\\w';
             $Mixed = Gdn_Format::replaceButProtectCodeBlocks('/(^|[\\s,\\.>\\(])@(' . $Pattern . '{1,64})\\b/i', '\\1' . anchor('@$2', $urlFormat), $Mixed);
         }
         // Handle #hashtag searches
         if (C('Garden.Format.Hashtags')) {
             $Mixed = Gdn_Format::replaceButProtectCodeBlocks('/(^|[\\s,\\.>])\\#([\\w\\-]+)(?=[\\s,\\.!?<]|$)/i', '\\1' . anchor('#\\2', '/search?Search=%23\\2&Mode=like') . '\\3', $Mixed);
         }
         // Handle "/me does x" action statements
         if (C('Garden.Format.MeActions')) {
             $Mixed = Gdn_Format::replaceButProtectCodeBlocks('/(^|[\\n])(\\/me)(\\s[^(\\n)]+)/i', '\\1' . wrap(wrap('\\2', 'span', array('class' => 'MeActionName')) . '\\3', 'span', array('class' => 'AuthorAction')), $Mixed);
         }
         return $Mixed;
     }
 }
Ejemplo n.º 2
0
 /**
  * Translate all emoji aliases to their corresponding Html image tags.
  *
  * Thanks to punbb 1.3.5 (GPL License) for function, which was largely
  * inspired from their do_smilies function.
  *
  * @param string $Text The actual user-submitted post
  * @return string Return the emoji-formatted post
  */
 public function translateToHtml($Text)
 {
     if (!$this->enabled) {
         return $Text;
     }
     $Text = ' ' . $Text . ' ';
     // First, translate all aliases. Canonical emoji will get translated
     // out of a loop.
     $emojiAliasList = $this->aliases;
     // Loop through and apply changes to all visible aliases from dropdown
     foreach ($emojiAliasList as $emojiAlias => $emojiCanonical) {
         $emojiFilePath = $this->getEmojiPath($emojiCanonical);
         if (strpos($Text, htmlentities($emojiAlias)) !== false) {
             $Text = Gdn_Format::ReplaceButProtectCodeBlocks('`(?<=[>\\s]|(&nbsp;))' . preg_quote(htmlentities($emojiAlias), '`') . '(?=\\W)`m', $this->img($emojiFilePath, $emojiAlias), $Text);
         }
     }
     // Second, translate canonical list, without looping.
     $ldelim = preg_quote($this->ldelim, '`');
     $rdelim = preg_quote($this->rdelim, '`');
     $emoji = $this;
     $Text = Gdn_Format::replaceButProtectCodeBlocks("`({$ldelim}[a-z0-9_+-]+{$rdelim})`i", function ($m) use($emoji) {
         $emoji_name = trim($m[1], ':');
         $emoji_path = $emoji->getEmojiPath($emoji_name);
         if ($emoji_path) {
             return $emoji->img($emoji_path, $emoji->ldelim . $emoji_name . $emoji->rdelim);
         } else {
             return $m[0];
         }
     }, $Text, true);
     return substr($Text, 1, -1);
 }
Ejemplo n.º 3
0
 /**
  * Handle mentions formatting.
  *
  * @param $Mixed
  * @return mixed|string
  */
 public static function mentions($Mixed)
 {
     if (!is_string($Mixed)) {
         return self::to($Mixed, 'Mentions');
     } else {
         // Check for a custom formatter.
         $Formatter = Gdn::factory('MentionsFormatter');
         if (is_object($Formatter)) {
             return $Formatter->formatMentions($Mixed);
         }
         // Handle @mentions.
         if (c('Garden.Format.Mentions')) {
             // Only format mentions that are not already in anchor tags or code tags.
             $Mixed = self::tagContent($Mixed, 'Gdn_Format::formatMentionsCallback');
         }
         // Handle #hashtag searches
         if (c('Garden.Format.Hashtags')) {
             $Mixed = Gdn_Format::replaceButProtectCodeBlocks('/(^|[\\s,\\.>])\\#([\\w\\-]+)(?=[\\s,\\.!?<]|$)/i', '\\1' . anchor('#\\2', '/search?Search=%23\\2&Mode=like') . '\\3', $Mixed);
         }
         // Handle "/me does x" action statements
         if (c('Garden.Format.MeActions')) {
             $Mixed = Gdn_Format::replaceButProtectCodeBlocks('/(^|[\\n])(\\/me)(\\s[^(\\n)]+)/i', '\\1' . wrap(wrap('\\2', 'span', array('class' => 'MeActionName')) . '\\3', 'span', array('class' => 'AuthorAction')), $Mixed);
         }
         return $Mixed;
     }
 }