首页 > 解决方案 > PHP中的非法字符串偏移警告

问题描述

我一直在尝试理解在实现我在网上获取的代码并对其进行修改以使 Wordpress [视频] 短代码响应之后遇到的一些错误,但我真的不知道在尝试后要更改什么来修复它一堆东​​西......这些是错误:

警告:第 41 行中的非法字符串偏移“高度”

警告:在...第 41 行遇到的非数字值

警告:在第 41 行...除以零

这是造成错误的代码的一部分:

$padding = ($meta['height']/$meta['width'])*100 - 25;

谁能帮助我理解和修复这些错误?这是完整的代码:

// OVERRIDE [VIDEO] SHORTCODE TO MAKE RESPONSIVE
// Thanks to: https://www.stirtingale.com/guides/2018/11/wordpress-video

function lookupIDfromURL($image_url) {
    // basic lookup from DB to match media URL with media URL
    global $wpdb;
    $attachment = $wpdb->get_col($wpdb->prepare("SELECT ID FROM $wpdb->posts WHERE guid='%s';", $image_url )); 
        return $attachment[0]; 
}

add_filter( 'wp_video_shortcode', function( $output ) {

    // get SRC 
    // this is a bit hacky

    preg_match( '@src="([^"]+)"@' , $output, $match );
    $src = array_pop($match);
    $src = preg_replace('/\?.*/', '', $src);

    // get ID

    $postid = lookupIDfromURL( $src );
    $meta = wp_get_attachment_metadata($postid);

    // let it autoplay 
    // and include playsinline to fix issues on iOS

    $output = str_replace( "<video", "<video playsinline autoplay muted loop ", $output );
    $output = str_replace( "controls=", "data-controls=", $output );
    
    // wrap it all up

    $str = preg_replace('/\<[\/]{0,1}div[^\>]*\>/i', '', $output);
    $padding = ($meta['height']/$meta['width'])*100 - 25; // modified to include narrower height
    $wrap = "<div class='embed-responsive' style='padding-bottom:". $padding ."%'>".$str."</div>";
    
    $output = $wrap;
    return $output;

} );

标签: phperror-handling

解决方案


推荐阅读