我在SQL中有一些值想要修剪和替换。
我想查看一个关键字段_booking_persons
,仅存储""
之间的所有内容,然后删除其余部分。
所以现在我在所有领域都知道了
s:14:"a:1:{i:0;i:4;}“
我想保留a:1:{i:0;i:4;}
,以便" "
之间的所有内容。
我试图用wordpress post_meta_update修复它。因此,我可能很难用SQL做到这一点。
$args = array('post_type' => 'wc_booking', 'posts_per_page' => '-1' );
$the_query = new WP_Query( $args );
// The Loop
if ( $the_query->have_posts() ) {
while ( $the_query->have_posts() ) {
$the_query->the_post();
$post_id = get_the_ID();
$array = get_post_meta( $post_id, '_booking_persons', true );
$between = preg_replace('/(.*)"(.*)"(.*)/sm', '\2', $array);
update_post_meta( $post_id, '_booking_persons', $between );
}
/* Restore original Post Data */
wp_reset_postdata();
} else {
// no posts found
}
我希望a:1:{i:0;i:4;}
,但保持不变s:14:"a:1:{i:0;i:4;}“
答案 0 :(得分:0)
您可以使用substring_index函数轻松完成此操作:
SELECT substring_index(substring_index('s:14:\“ a:1:{i:0; i:4;} \”','\“',2),'\”',-1);
这将返回:
a:1:{i:0; i:4;}
请注意,您需要使用\
转义“”字符您可以在运行中here看到它