Title: WP_HTML_Decoder::attribute_starts_with
Published: July 16, 2024
Last modified: February 24, 2026

---

# WP_HTML_Decoder::attribute_starts_with( string $haystack, string $search_text, string $case_sensitivity ): bool

## In this article

 * [Description](https://developer.wordpress.org/reference/classes/wp_html_decoder/attribute_starts_with/?output_format=md#description)
 * [Parameters](https://developer.wordpress.org/reference/classes/wp_html_decoder/attribute_starts_with/?output_format=md#parameters)
 * [Return](https://developer.wordpress.org/reference/classes/wp_html_decoder/attribute_starts_with/?output_format=md#return)
 * [Source](https://developer.wordpress.org/reference/classes/wp_html_decoder/attribute_starts_with/?output_format=md#source)
 * [Related](https://developer.wordpress.org/reference/classes/wp_html_decoder/attribute_starts_with/?output_format=md#related)
 * [Changelog](https://developer.wordpress.org/reference/classes/wp_html_decoder/attribute_starts_with/?output_format=md#changelog)

[ Back to top](https://developer.wordpress.org/reference/classes/wp_html_decoder/attribute_starts_with/?output_format=md#wp--skip-link--target)

Indicates if an attribute value starts with a given raw string value.

## 󠀁[Description](https://developer.wordpress.org/reference/classes/wp_html_decoder/attribute_starts_with/?output_format=md#description)󠁿

Use this method to determine if an attribute value starts with a given string, regardless
of how it might be encoded in HTML. For instance, `http:` could be represented as`
http:` or as `http&colon;` or as `&#x68;ttp:` or as `h&#116;tp&colon;`, or in many
other ways.

Example:

    ```php
    $value = 'http&colon;//wordpress.org/';
    true   === WP_HTML_Decoder::attribute_starts_with( $value, 'http:', 'ascii-case-insensitive' );
    false  === WP_HTML_Decoder::attribute_starts_with( $value, 'https:', 'ascii-case-insensitive' );
    ```

## 󠀁[Parameters](https://developer.wordpress.org/reference/classes/wp_html_decoder/attribute_starts_with/?output_format=md#parameters)󠁿

 `$haystack`stringrequired

String containing the raw non-decoded attribute value.

`$search_text`stringrequired

Does the attribute value start with this plain string.

`$case_sensitivity`stringoptional

Pass `'ascii-case-insensitive'` to ignore ASCII case when matching.
 Default `'case-
sensitive'`.

## 󠀁[Return](https://developer.wordpress.org/reference/classes/wp_html_decoder/attribute_starts_with/?output_format=md#return)󠁿

 bool Whether the attribute value starts with the given string.

## 󠀁[Source](https://developer.wordpress.org/reference/classes/wp_html_decoder/attribute_starts_with/?output_format=md#source)󠁿

    ```php
    public static function attribute_starts_with( $haystack, $search_text, $case_sensitivity = 'case-sensitive' ): bool {
    	$search_length = strlen( $search_text );
    	$loose_case    = 'ascii-case-insensitive' === $case_sensitivity;
    	$haystack_end  = strlen( $haystack );
    	$search_at     = 0;
    	$haystack_at   = 0;

    	while ( $search_at < $search_length && $haystack_at < $haystack_end ) {
    		$chars_match = $loose_case
    			? strtolower( $haystack[ $haystack_at ] ) === strtolower( $search_text[ $search_at ] )
    			: $haystack[ $haystack_at ] === $search_text[ $search_at ];

    		$is_introducer = '&' === $haystack[ $haystack_at ];
    		$next_chunk    = $is_introducer
    			? self::read_character_reference( 'attribute', $haystack, $haystack_at, $token_length )
    			: null;

    		// If there's no character reference and the characters don't match, the match fails.
    		if ( null === $next_chunk && ! $chars_match ) {
    			return false;
    		}

    		// If there's no character reference but the character do match, then it could still match.
    		if ( null === $next_chunk && $chars_match ) {
    			++$haystack_at;
    			++$search_at;
    			continue;
    		}

    		// If there is a character reference, then the decoded value must exactly match what follows in the search string.
    		if ( 0 !== substr_compare( $search_text, $next_chunk, $search_at, strlen( $next_chunk ), $loose_case ) ) {
    			return false;
    		}

    		// The character reference matched, so continue checking.
    		$haystack_at += $token_length;
    		$search_at   += strlen( $next_chunk );
    	}

    	return true;
    }
    ```

[View all references](https://developer.wordpress.org/reference/files/wp-includes/html-api/class-wp-html-decoder.php/)
[View on Trac](https://core.trac.wordpress.org/browser/tags/6.9.4/src/wp-includes/html-api/class-wp-html-decoder.php#L34)
[View on GitHub](https://github.com/WordPress/wordpress-develop/blob/6.9.4/src/wp-includes/html-api/class-wp-html-decoder.php#L34-L74)

## 󠀁[Related](https://developer.wordpress.org/reference/classes/wp_html_decoder/attribute_starts_with/?output_format=md#related)󠁿

| Uses | Description | 
| [WP_HTML_Decoder::read_character_reference()](https://developer.wordpress.org/reference/classes/wp_html_decoder/read_character_reference/)`wp-includes/html-api/class-wp-html-decoder.php` |

Attempt to read a character reference at the given location in a given string, depending on the context in which it’s found.

  |

## 󠀁[Changelog](https://developer.wordpress.org/reference/classes/wp_html_decoder/attribute_starts_with/?output_format=md#changelog)󠁿

| Version | Description | 
| [6.6.0](https://developer.wordpress.org/reference/since/6.6.0/) | Introduced. |

## User Contributed Notes

You must [log in](https://login.wordpress.org/?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Fclasses%2Fwp_html_decoder%2Fattribute_starts_with%2F)
before being able to contribute a note or feedback.