/** * Evaluate the shortcodes on some text * * @param string $content Text containing shortcodes (or none) * @param array $passatt Attributes to be passed to the shortcode handler callbacks. * @return string The transformed text. */ public static function apply($content, $passatt = NULL) { if ($passatt !== NULL) { self::$passatt = $passatt; } $pattern = self::getRegex(); // we want to split the message into nice paragraphs first. Don't want // to interfere with shortcode tags or the content between them, so // translate the tags into keys... $keysuffix = uniqid(); $keyed = array(); // replace \r\n style line breaks with \n $content = str_replace("\r\n", "\n", $content); preg_match_all("/{$pattern}/s", $content, $match); foreach ($match[0] as $match) { $key = md5($match . $keysuffix); $keyed[$key] = $match; $content = str_replace($match, $key, $content); } // split by two or more new lines/carriage returns $lines = preg_split("#[\n\r]{2,}#", $content, NULL, PREG_SPLIT_DELIM_CAPTURE); // reconstruct as a sequence of paragraphs: $trans_content = ""; if (count($lines) > 1) { foreach ($lines as $line) { $trans_content .= "[_p]" . trim($line) . "[/_p]"; } } else { // [_p] will call us back... when we get to one line after evaluating // shortcodes inside the [_p], we stop adding [_p]'s. $trans_content = $lines[0]; } // replace back the tag keys with the actual tags: foreach ($keyed as $key => $match) { $trans_content = str_replace($key, $match, $trans_content); } // evaluate the shortcodes (including the paragraphs we just created). return trim(preg_replace_callback("/{$pattern}/s", array("Bbpp_ThankMeLater_Shortcoder", "applyTag"), $trans_content)); }