BP_Media_Extractor::extract_links( string $richtext, string $plaintext, array $extra_args = array() )

Extract <a href> tags from text.

Description

Parameters

$richtext

(Required) Content to parse.

$plaintext

(Required) Sanitized version of the content.

$extra_args

(Optional) Bespoke data for a particular extractor (optional).

Default value: array()

Return

(array)

  • 'has'
    (array) Extracted media counts. {
  • 'links'
    (int)
  • 'links'
    (array) Extracted URLs. { Array of extracted media.
  • 'url'
    (string) Link.
  • Source

    File: bp-core/classes/class-bp-media-extractor.php

    208
    209
    210
    211
    212
    213
    214
    215
    216
    217
    218
    219
    220
    221
    222
    223
    224
    225
    226
    227
    228
    229
    230
    231
    232
    233
    234
    235
    236
    237
    238
    239
    240
    241
    protected function extract_links( $richtext, $plaintext, $extra_args = array() ) {
        $data = array( 'has' => array( 'links' => 0 ), 'links' => array() );
     
        // Matches: href="text" and href='text'.
        if ( stripos( $richtext, 'href=' ) !== false ) {
            preg_match_all( '#href=(["\'])([^"\']+)\1#i', $richtext, $matches );
     
            if ( ! empty( $matches[2] ) ) {
                $matches[2] = array_unique( $matches[2] );
     
                foreach ( $matches[2] as $link_src ) {
                    $link_src = esc_url_raw( $link_src );
     
                    if ( $link_src ) {
                        $data['links'][] = array( 'url' => $link_src );
                    }
                }
            }
        }
     
        $data['has']['links'] = count( $data['links'] );
     
        /**
         * Filters links extracted from text.
         *
         * @since BuddyPress 2.3.0
         *
         * @param array  $data       Extracted links. See {@link BP_Media_Extractor::extract_links()} for format.
         * @param string $richtext   Content to parse.
         * @param string $plaintext  Copy of $richtext without any markup.
         * @param array  $extra_args Bespoke data for a particular extractor.
         */
        return apply_filters( 'bp_media_extractor_links', $data, $richtext, $plaintext, $extra_args );
    }

    Changelog

    Changelog
    Version Description
    BuddyPress 2.3.0 Introduced.

    Questions?

    We're always happy to help with code or other questions you might have! Search our developer docs, contact support, or connect with our sales team.