wordpress收费下载资源主题
wordpress付费下载资源插件

WordPress函数get_shortcode_regex详细用法教程解析

get_shortcode_regex函数是WordPress中的一个函数,用于获取一个正则表达式,匹配所有的短代码(shortcode)。

该函数的定义如下:

function get_shortcode_regex() {
$tagregexp = join( '|', array_map( 'preg_quote', array( 'gallery', 'caption', 'embed', 'video', 'audio', 'playlist', 'wp_caption', 'caption', 'embed', 'gallery', 'playlist', 'video', 'audio', 'embed', 'wp_caption' ) ) );

// 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]]
}

该函数返回一个正则表达式,用于匹配短代码。

这个正则表达式包括以下几个部分:

1. `\[`:匹配一个开方括号 `[`。
2. `(\[?)`:可选的第二个开方括号,用于转义短代码。
3. `($tagregexp)`:短代码名称。
4. `(?![\w-])`:不匹配字母、数字或连字符,用于确保短代码名称后没有其他字符。
5. `(`:开始捕获短代码的内容。
6. `[^\]\/]*`:匹配不是闭方括号或正斜杠的任意字符。
7. `(?:\/(?!\])[^\]\/]*)*?`:可选的匹配,用于匹配短代码内的正斜杠。
8. `(\/)`:捕获可选的自闭合标签的斜杠。
9. `\]`:匹配闭方括号 `]`。
10. `|`:或操作符,用于匹配自闭合标签和非自闭合标签。
11. `\]`:匹配闭方括号 `]`。
12. `(`:开始捕获短代码之间的内容。
13. `[^\[]*+`:匹配不是开方括号的任意字符。
14. `(?:\[(?!\/\2\])[^\[]*+)*+`:可选的匹配,用于匹配不是开方括号的任意字符,但不能是关闭短代码标签的开方括号。
15. `\[\/\2\]`:匹配闭短代码标签。
16. `)`:结束捕获短代码之间的内容。
17. `)?`:可选的捕获短代码之间的内容。
18. `(\]?)`:可选的第二个闭方括号,用于转义短代码。

通过调用get_shortcode_regex()函数,可以得到一个用于匹配短代码的正则表达式。然后可以使用这个正则表达式在文本中查找短代码,并进行相应的处理。

0 个评论

定制开发
本站承接WordPress等系统建站仿站、二次开发、主题插件定制等开发服务
在线咨询
  • 请直接说明需求,勿问在否
    QQ:1-247-246-247

  • QQ一群:104228692(满)
  • QQ二群:64786792
在线咨询
本站承接WordPress建站仿站、二次开发、主题插件定制等PHP开发服务!

了解详情