Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/wordpress/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/vim/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 使用preg_replace删除精确嵌入代码(wordpress)_Php_Wordpress_Preg Replace_Preg Match_Embed - Fatal编程技术网

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

Php 使用preg_replace删除精确嵌入代码(wordpress),php,wordpress,preg-replace,preg-match,embed,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 =

我使用以下代码查找帖子内容中嵌入的第一个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) {

    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');
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');