使用preg_replace删除精确的嵌入代码(wordpress)

时间:2015-03-27 01:34:15

标签: php wordpress preg-replace preg-match embed

我使用以下代码查找嵌入帖子内容中的第一个YouTube / Vimeo:

function compare_by_offset( $a, $b ) {
    return $a['order'] - $b['order'];
}

function first_video_url($post_id = null) {

    if ( $post_id == null OR $post_id == '' ) $post_id = get_the_ID();

    $post_array = get_post( $post_id );

    $markup = $post_array->post_content;

    $regexes = array(
        '#(?:https?:)?//www\.youtube(?:\-nocookie)?\.com/(?:v|e|embed)/([A-Za-z0-9\-_]+)#', // Comprehensive search for both iFrame and old school embeds
        '#(?:https?(?:a|vh?)?://)?(?:www\.)?youtube(?:\-nocookie)?\.com/watch\?.*v=([A-Za-z0-9\-_]+)#', // Any YouTube URL. After http(s) support a or v for Youtube Lyte and v or vh for Smart Youtube plugin
        '#(?:https?(?:a|vh?)?://)?youtu\.be/([A-Za-z0-9\-_]+)#', // Any shortened youtu.be URL. After http(s) a or v for Youtube Lyte and v or vh for Smart Youtube plugin
        '#<div class="lyte" id="([A-Za-z0-9\-_]+)"#', // YouTube Lyte
        '#data-youtube-id="([A-Za-z0-9\-_]+)"#', // LazyYT.js
        '#<object[^>]+>.+?http://vimeo\.com/moogaloop.swf\?clip_id=([A-Za-z0-9\-_]+)&.+?</object>#s', // Standard Vimeo embed code
        '#(?:https?:)?//player\.vimeo\.com/video/([0-9]+)#', // Vimeo iframe player
        '#\[vimeo id=([A-Za-z0-9\-_]+)]#', // JR_embed shortcode
        '#\[vimeo clip_id="([A-Za-z0-9\-_]+)"[^>]*]#', // Another shortcode
        '#\[vimeo video_id="([A-Za-z0-9\-_]+)"[^>]*]#', // Yet another shortcode
        '#(?:https?://)?(?:www\.)?vimeo\.com/([0-9]+)#', // Vimeo URL
        '#(?:https?://)?(?:www\.)?vimeo\.com/channels/(?:[A-Za-z0-9]+)/([0-9]+)#' // Channel URL
    );


    $provider_videos = array();

    foreach ( $regexes as $regex ) {
        if ( preg_match_all( $regex, $markup, $matches, PREG_OFFSET_CAPTURE ) ) {
            $provider_videos = array_merge( $provider_videos, $matches[0] );
        }
    }

    if ( empty( $provider_videos ) ) return;

    foreach ( $provider_videos as $video ) {
        $videos[] = array(
            'url'    => $video[0],
            'order'  => $video[1]
        );
    }

    usort( $videos, 'compare_by_offset' );

    $first_video_url =  current(array_column($videos, 'url'));

    if ( empty( $first_video_url ) ) return;

    return $first_video_url;

}

现在,当我收到帖子中第一个视频的链接时,我想将其从帖子内容中删除。那就是我被困的地方。到目前为止我的尝试:

function remove_first_image ($content) {

    $url = first_video_url();
    $parsed = parse_url($url);
    $video_id = $parsed['query'];
    $embed_code = wp_oembed_get($url);

    $pattern = 'a pattern for that embed which I fail to make';

    $content = preg_replace($pattern, '', $content);

    return $content;
}
add_filter('the_content', 'remove_first_image');

谢谢!

1 个答案:

答案 0 :(得分:0)

我想在他问这个问题之前,我无法回答他自己的愚蠢问题。答案就是:

function remove_first_image ($content) {

    if ( is_single() && has_post_format('video') ) {

        $url = first_video_url();
        $embed_code = wp_oembed_get($url);

        $content = str_replace($embed_code, '', $content);
    } 

    return $content;
}

add_filter('the_content', 'remove_first_image');