从wp_postmeta中序列化数组的字符串中删除“%”

时间:2011-03-30 作者:two7s_clash

在将博客从Blogger转换为WP并运行脚本获取热门链接图片以进行托管的过程中,我最终得到了一些时髦的图片名称,如

act%252Bapandas-210x290.png
这些图像名称会阻止图像显示在网页上,因为url编码最终会出现在文件名本身中(不要问!)。我在文件服务器上重命名了它们,没有问题,但名称也在每个帖子的附件元数据中。

如何从wp\\U Posteta表中的所有图像引用中删除“%”*其中大多数出现在\\u wp\\u attachment\\u metadata*的meta\\u键的meta\\u值中的序列化数组中。我没有找到一个插件,也不知道如何建立一个纯SQL/PHP解决方案。

下面是一个序列化数组条目的示例(Smush.it插件,ugh进一步搞糟了):

a:7:{s:5:"width";s:3:"210";s:6:"height";s:3:"339";s:14:"hwstring_small";s:22:"height=\'96\' width=\'59\'";s:4:"file";s:27:"2011/02/act%252Bapandas.png";s:5:"sizes";a:6:{s:9:"thumbnail";a:4:{s:4:"file";s:27:"act%252Bapandas-210x290.png";s:5:"width";s:3:"210";s:6:"height";s:3:"290";s:10:"wp_smushit";s:271:"Smush.it error: Could not get the image while processing http://new.xxxxx.com/wp-content/uploads/2011/02/act%252Bapandas-210x290.png (/home/xxxxxxxxx/new.xxxxx.com/wp-content/uploads/2011/02/act%252Bapandas-210x290.png)";}s:14:"soft-thumbnail";a:4:{s:4:"file";s:27:"act%252Bapandas-179x290.png";s:5:"width";s:3:"179";s:6:"height";s:3:"290";s:10:"wp_smushit";s:271:"Smush.it error: Could not get the image while processing http://new.xxxxx.com/wp-content/uploads/2011/02/act%252Bapandas-179x290.png (/home/xxxxxxxxx/new.xxxxx.com/wp-content/uploads/2011/02/act%252Bapandas-179x290.png)";}s:14:"mini-thumbnail";a:4:{s:4:"file";s:25:"act%252Bapandas-60x60.png";s:5:"width";s:2:"60";s:6:"height";s:2:"60";s:10:"wp_smushit";s:267:"Smush.it error: Could not get the image while processing http://new.xxxxx.com/wp-content/uploads/2011/02/act%252Bapandas-60x60.png (/home/xxxxxxxxx/new.xxxxx.com/wp-content/uploads/2011/02/act%252Bapandas-60x60.png)";}s:5:"slide";a:4:{s:4:"file";s:27:"act%252Bapandas-210x290.png";s:5:"width";s:3:"210";s:6:"height";s:3:"290";s:10:"wp_smushit";s:271:"Smush.it error: Could not get the image while processing http://new.xxxxx.com/wp-content/uploads/2011/02/act%252Bapandas-210x290.png (/home/xxxxxxxxx/new.xxxxx.com/wp-content/uploads/2011/02/act%252Bapandas-210x290.png)";}s:10:"soft-slide";a:4:{s:4:"file";s:27:"act%252Bapandas-179x290.png";s:5:"width";s:3:"179";s:6:"height";s:3:"290";s:10:"wp_smushit";s:271:"Smush.it error: Could not get the image while processing http://new.xxxxx.com/wp-content/uploads/2011/02/act%252Bapandas-179x290.png (/home/xxxxxxxxx/new.xxxxx.com/wp-content/uploads/2011/02/act%252Bapandas-179x290.png)";}s:10:"mini-slide";a:4:{s:4:"file";s:27:"act%252Bapandas-210x145.png";s:5:"width";s:3:"210";s:6:"height";s:3:"145";s:10:"wp_smushit";s:271:"Smush.it error: Could not get the image while processing http://new.xxxxx.com/wp-content/uploads/2011/02/act%252Bapandas-210x145.png (/home/xxxxxxxxx/new.xxxxx.com/wp-content/uploads/2011/02/act%252Bapandas-210x145.png)";}}s:10:"image_meta";a:10:{s:8:"aperture";s:1:"0";s:6:"credit";s:0:"";s:6:"camera";s:0:"";s:7:"caption";s:0:"";s:17:"created_timestamp";s:1:"0";s:9:"copyright";s:0:"";s:12:"focal_length";s:1:"0";s:3:"iso";s:1:"0";s:13:"shutter_speed";s:1:"0";s:5:"title";s:0:"";}s:10:"wp_smushit";s:255:"Smush.it error: Could not get the image while processing http://new.xxxxx.com/wp-content/uploads/2011/02/act%252Bapandas.png (/home/xxxxxxxxx/new.xxxxx.com/wp-content/uploads/2011/02/act%252Bapandas.png)";}
问题是更改或删除“%”字符并更新数组,以便它报告正确的字符数(即s:13表示yoursite.com是13个字符[]),我也愿意使用php解决方案!只要能帮我解决这个烂摊子。

FINAL SOLUTION

请参见下面我的答案。

2 个回复
最合适的回答,由SO网友:Rarst 整理而成

一般的想法是遍历所有附件来检索、修改和写回它们的元。

类似这样的内容(在使用任何重要设备之前进行彻底测试):

$posts = get_posts(array(
    \'post_type\' => \'attachment\',
    \'numberposts\' => -1,
));

foreach( $posts as $post ) {

    // retrieve data, unserialized automatically
    $meta = get_post_meta($post->ID,\'_wp_attachment_metadata\', true);

    // do stuff with $meta array

    // write it back
    update_post_meta($post->ID, \'_wp_attachment_metadata\', $meta);
}

SO网友:two7s_clash
  $posts = get_posts(array(
                \'post_type\' => \'attachment\',
                \'numberposts\' => -1,
            ));

    // Recursive String Replace - recursive_array_replace(mixed, mixed, array);
    function recursive_array_replace($find, $replace, $array){
    if (!is_array($array)) {
    return str_replace($find, $replace, $array);
    }
    $newArray = array();
    foreach ($array as $key => $value) {
    $newArray[$key] = recursive_array_replace($find, $replace, $value);
    }
    return $newArray;
    }

    foreach ($posts as $post) {
        // retrieve data, unserialized automatically
        $meta = get_post_meta($post->ID, \'_wp_attachment_metadata\', true);
        // see if file name has "%" in it
        if (strpos($meta[file], "%")) {
        //recursively scrub "%"
        $meta = recursive_array_replace(\'%\', \'\', $meta);
        }

        // write it back
        update_post_meta($post->ID, \'_wp_attachment_metadata\', $meta);
    }
结束

相关推荐

当我试图停用任何插件时,它会给出错误“WARNING:CALL_USER_FUNC_ARRAY()[Function.Call-User-Func-ARRAY]”

当我尝试取消激活任何插件时,它会出错。如何解决这个问题?插件被取消激活,但单击按钮“Deactivate”时,我收到此错误。警告:call\\u user\\u func\\u array()[函数.call user func array]:第一个参数应该是有效的回调,“youtuber\\u uninstall”在/home/username/public\\u html/wp includes/plugin中给出。php在线395**