https://core.trac.wordpress.org/browser/tags/4.0/src/wp-includes/shortcodes.php#L228包括用於分析的簡碼的代碼...
function get_shortcode_regex() {
global $shortcode_tags;
$tagnames = array_keys($shortcode_tags);
$tagregexp = join('|', array_map('preg_quote', $tagnames));
// WARNING! Do not change this regex without changing do_shortcode_tag() and strip_shortcode_tag()
// Also, see shortcode_unautop() and shortcode.js.
return
'\\[' // Opening bracket
. '(\\[?)' // 1: Optional second opening bracket for escaping shortcodes: [[tag]]
. "($tagregexp)" // 2: Shortcode name
. '(?![\\w-])' // Not followed by word character or hyphen
. '(' // 3: Unroll the loop: Inside the opening shortcode tag
. '[^\\]\\/]*' // Not a closing bracket or forward slash
. '(?:'
. '\\/(?!\\])' // A forward slash not followed by a closing bracket
. '[^\\]\\/]*' // Not a closing bracket or forward slash
. ')*?'
. ')'
. '(?:'
. '(\\/)' // 4: Self closing tag ...
. '\\]' // ... and closing bracket
. '|'
. '\\]' // Closing bracket
. '(?:'
. '(' // 5: Unroll the loop: Optionally, anything between the opening and closing shortcode tags
. '[^\\[]*+' // Not an opening bracket
. '(?:'
. '\\[(?!\\/\\2\\])' // An opening bracket not followed by the closing shortcode tag
. '[^\\[]*+' // Not an opening bracket
. ')*+'
. ')'
. '\\[\\/\\2\\]' // Closing shortcode tag
. ')?'
. ')'
. '(\\]?)'; // 6: Optional second closing brocket for escaping shortcodes: [[tag]]
}
我相信這是你想要的,因爲它顯示了他們目前是如何解析的短代碼,並在此行他們實現調用函數https://core.trac.wordpress.org/browser/tags/4.0/src/wp-includes/shortcodes.php#L277 ...
function do_shortcode_tag($m) {
global $shortcode_tags;
// allow [[foo]] syntax for escaping a tag
if ($m[1] == '[' && $m[6] == ']') {
return substr($m[0], 1, -1);
}
$tag = $m[2];
$attr = shortcode_parse_atts($m[3]);
if (isset($m[5])) {
// enclosing tag - extra parameter
return $m[1] . call_user_func($shortcode_tags[$tag], $attr, $m[5], $tag) . $m[6];
} else {
// self-closing tag
return $m[1] . call_user_func($shortcode_tags[$tag], $attr, null, $tag) . $m[6];
}
}
說實話,我只是下載wordpress的一個副本,並竊取wp-includes/shortcodes.php文件,並節省自己的麻煩或重新實現這一點。
http://codex.wordpress.org/Sortort_API – mschuett 2014-09-26 19:52:11
我問他們如何在PHP中實現簡碼,而不是問如何使用wordpress的簡碼。我試圖開發一個網站,使用短代碼,而不是使用wordpress – Cliffmeister 2014-09-26 19:54:00
o那麼請更新你的問題,因爲你剛纔所說,你發佈的是一個完全不同的問題。 – mschuett 2014-09-26 19:55:41