我需要一些帮助来解决这个问题:
我有一个包含短代码的字符串,如[shortcode data="1" data2="2"]
如何使用regex提取?没有wordpress系统?
提前谢谢。
我需要一些帮助来解决这个问题:
我有一个包含短代码的字符串,如[shortcode data="1" data2="2"]
如何使用regex提取?没有wordpress系统?
提前谢谢。
这个字符串位于哪里?我知道你写了“没有wordpress系统”,但wordpress有一个功能get shortcode regex
如果出于某种原因确实想避免,以下示例可能会帮助您:
// check the post for a short code
function check_shortcode( $shortcode = NULL ) {
$post_to_check = get_post( get_the_ID() );
// Until we search, set false
$found = false;
// no short - FALSE
if ( ! $shortcode ) {
return $found;
}
// check the post content for the short code
if ( stripos( $post_to_check->post_content, \'[\' . $shortcode) !== FALSE ) {
// we have found the short code
$found = TRUE;
}
// return our final results
return $found;
}
法典中有an example 使用get\\u shortcode\\u regex()检查是否在给定页面上调用了短代码:$pattern = get_shortcode_regex(); preg_match(\'/\'.$pattern.\'/s\', $posts[0]->post_content, $matches); if (is_array($matches) && $matches[2] == \'YOURSHORTCODE\') { /