shortcode_parse_atts

函式
shortcode_parse_atts ( $text )
引數
  • (string) $text
    Required:
返回值
  • (array|string) List of attribute values. Returns empty array if '""' === trim( $text ). Returns empty string if '' === trim( $text ). All other matches are checked for not empty().
定義位置
相關方法
shortcode_attsshortcode_existsshortcode_unautopget_shortcode_atts_regexrest_parse_date
引入
2.5.0
棄用
-

shortcode_parse_atts: 這個函式用來解析一個短碼的屬性。它需要一個引數 – $text – 這是短碼屬性的文字: 這個函式將返回一個代表短碼屬性的鍵/值對的陣列。

從短程式碼標籤中檢索所有屬性。

屬性列表將屬性名稱作為鍵,將屬性的值作為鍵/值對中的值。這允許更容易地檢索屬性,因為所有屬性都必須是已知的。

function shortcode_parse_atts( $text ) {
	$atts    = array();
	$pattern = get_shortcode_atts_regex();
	$text    = preg_replace( "/[x{00a0}x{200b}]+/u", ' ', $text );
	if ( preg_match_all( $pattern, $text, $match, PREG_SET_ORDER ) ) {
		foreach ( $match as $m ) {
			if ( ! empty( $m[1] ) ) {
				$atts[ strtolower( $m[1] ) ] = stripcslashes( $m[2] );
			} elseif ( ! empty( $m[3] ) ) {
				$atts[ strtolower( $m[3] ) ] = stripcslashes( $m[4] );
			} elseif ( ! empty( $m[5] ) ) {
				$atts[ strtolower( $m[5] ) ] = stripcslashes( $m[6] );
			} elseif ( isset( $m[7] ) && strlen( $m[7] ) ) {
				$atts[] = stripcslashes( $m[7] );
			} elseif ( isset( $m[8] ) && strlen( $m[8] ) ) {
				$atts[] = stripcslashes( $m[8] );
			} elseif ( isset( $m[9] ) ) {
				$atts[] = stripcslashes( $m[9] );
			}
		}

		// Reject any unclosed HTML elements.
		foreach ( $atts as &$value ) {
			if ( false !== strpos( $value, '<' ) ) {
				if ( 1 !== preg_match( '/^[^<]*+(?:<[^>]*+>[^<]*+)*+$/', $value ) ) {
					$value = '';
				}
			}
		}
	} else {
		$atts = ltrim( $text );
	}

	return $atts;
}

常見問題

FAQs
檢視更多 >