从非常老的版本更新到PHP 7.4后,插件preg_match_all错误

时间:2020-10-10 22:47:53

标签: php regex preg-match-all character-class

我们的网站错误打开(插件已激活):

示例URL错误: https://www.live-karikaturen.ch/downloads/1-august-fahnen-schwingen-nationalfeiertag-schweiz/ https://www.live-karikaturen.ch/webshop-gratisbilder-free-cartoon-comic-images/ https://www.live-karikaturen.ch/downloads/auto-suv-autofan-abgase-umweltverschmutzung/

不兼容更新:我从一个很旧的PHP版本更新到7.4

我使用了这个非常重要的插件,它已经过时了(最近更新为4年),非常可怕: https://wordpress.org/support/plugin/creative-commons-configurator-1/

现在我遇到了这个错误,我认为它可能很容易解决,但是我对PHP几乎一无所知,但是我尝试了一下,所以在此先非常感谢。

错误: *

警告:preg_match_all():编译失败:转义序列为 字符类中的偏移量18 in无效 /home/clients/f9abb707fe4ac1215fe0f0a9c0b0ae21/www.live-karikaturen-ch/wp-content/plugins/creative-commons-configurator-1/creative-commons-configurator-1.php 在第821行

我从此处复制.php文件中的代码

CODE:这是第821行:

if ( ! preg_match_all( $pattern_images_no_caption, $post_content, $matches ) ) {
return $post_content;

}

代码从第803-828行开始(不知道复制行号的人):

function bccl_separate_licensing_on_images_without_caption( $post_content ) {

    // Plugin options
    $options = get_option('cc_settings');
    if ( $options === false || $options['cc_enable_individual_media_licensing'] == '0' ) {
        return $post_content;
    }

    $post = get_queried_object();

    if ( apply_filters('bccl_exclude_license', false, $post) ) {
        return $post_content;
    }

    // Pattern that matches images without caption
    //$pattern_images_no_caption = '#<p>[\s\R]*(<img [^>]+>)#';
    $pattern_images_no_caption = '#<(?:p|h[\d]+)>[\s\R]*(<img [^>]+>)#';
    $pattern_images_no_caption = apply_filters('bccl_pattern_match_images_without_caption' , $pattern_images_no_caption);
    **if ( ! preg_match_all( $pattern_images_no_caption, $post_content, $matches ) ) {
        return $post_content;
    }**
    //var_dump($matches[1]);

    // Iterate over the found images and add licensing metadata

    foreach ( $matches[1] as $image_html ) {

有一种简单的方法可以解决此错误吗?

非常感谢您的答复和帮助!

1 个答案:

答案 0 :(得分:-1)

非常感谢您,您真了不起! :)我尝试了一下,它可以正常工作,至少现在没有错误了。

第一 我只是不确定应该采用哪个版本的更正代码。我猜是1可以,不带[]。这就是说,正如Toto上文所述,我用一个简单的\ s代替了[\ s \ R]。

Nr。 1.) []

// $ pattern_images_no_caption ='#

\ s *(] +>)#'; $ pattern_images_no_caption ='#<(?: p | h [\ d] +)> \ s *(] +>)#';

Nr。 2.)与[]

//$pattern_images_no_caption = '#<p>[\s]*(<img [^>]+>)#';
    $pattern_images_no_caption = '#<(?:p|h[\d]+)>[\s]*(<img [^>]+>)#';

第二 我搜索了整个creative-commons-configurator.php插件代码中的\ R,然后在979行中进一步找到了这一点:

    $parts = preg_split('#\R#u', $attach`enter code here`ment_data);

这部分的所有行都是这样的:

// Attachment ID
    // Unfortunately, WP does not provide enough information in order to determine the attachment ID
    // So, first collect all the audio/video media file URLs in $attachments_urls
    $attachments_data = get_post_meta( $post->ID, 'enclosure', false );
    //var_dump($attachments_data);
    $attachments_urls = array();
    foreach ( $attachments_data as $attachment_data ) {
        $parts = preg_split('#\R#u', $attachment_data);
        $attachments_urls[] = $parts[0];
    }
    //var_dump($attachments_urls);
    // Then check which media file URL exists in the $atts array so as to
    // determine which attachment we are processing currently.
    $atts_values = array_values($atts);
    //var_dump($atts_values);
    // Find the URL of the attachment we are processing
    $current_attachment_url = '';
    foreach ( $attachments_urls as $attachment_url) {
        if ( in_array($attachment_url, $atts_values) ) {
            $current_attachment_url = $attachment_url;
            break;
        }
    }

问题:目前我没有收到任何错误,但是我想知道这是否会产生错误,因此我必须删除或替换“邪恶的” \ R吗?