如何在更改字符串后删除字符串中的额外逗号?

时间:2017-08-13 18:01:17

标签: php string

我有一个字符串可以跟踪用户ID作为关注者列表,它看起来像这样:

 1,2,3,4,5,6,7,8,9,10,... etc..

我有两个函数,一个用于添加新关注者(使用新数字更新字符串):

function my_followers_processor() {

    // post author ID
    $author_id  = $_POST['author_id'];
    $author_n   = $_POST['author_n'];

    // current user ID
    $current_id = $_POST['current_id'];

    // get current id followers meta
    $key        = 'followers';
    $single     = 'true';
    $current_f  =   get_user_meta($current_id, $key, $single);

    // update the string to include the new follower
    $new_follow = $current_f . ',' . $author_id;

    // update the users meta
    update_user_meta( $current_id, $key, $new_follow);

    // redifrect when were done
    wp_redirect( site_url() . '/followers/?follow=' . $author_id);

    // end
    die();

}

和一个删除关注者(更新字符串以从列表中删除ID):

function my_unfollowers_processor() {

    // post author ID
    $author_id  = $_POST['author_id'];
    $author_n   = $_POST['author_n'];

    // current user ID
    $current_id = $_POST['current_id'];

    // get current id followers meta
    $key        = 'followers';
    $single     = 'true';
    $current_f  =   get_user_meta($current_id, $key, $single);

    // just to make it easier to understand, set an unfriend var
    $unfriend   =   $author_id;

    // update the string to remove follower
    $new_follow =   str_replace($unfriend,'', $current_f);

    // update user meta
    update_user_meta( $current_id, $key, $new_follow);

    // redifrect when were done
    wp_redirect( site_url() . '/followers/?unfollow=' . $unfriend );

    // end
    die();
}

经过一些添加和删除的朋友后,我最终得到了一个类似于这样的字符串:

3,,5,,,,,1,,,,

当我删除一个朋友时,我试图str_replace额外的逗号,但是当我添加一个新朋友时,字符串中的最后两个数字之间没有逗号,然后我也尝试了添加逗号的反面,但后来我最终得到了两个逗号。

我需要添加一个或两个函数来清理那些额外的逗号并使用一个逗号分隔值维护一个列表(如顶部的示例列表)?

2 个答案:

答案 0 :(得分:0)

处理完成后,取出字符串并使用explode将其拆分,然后使用非空元素重建它。

<?php
$string = "3,,5,,,,,1,,,,";
$elements = explode(",", $string);
$newstring ="";
foreach ($elements as $pos) {
  if ($pos != '') {
    if ($newstring != "") $newstring .= ",";
    $newstring .= $pos;
  }
}
print_r($newstring);

答案 1 :(得分:0)

使用数组(通过explode跟随者的字符串)来添加新的关注者和/或删除现有的关注者,而不是使用字符串操纵关注者。稍后,在更新新关注者列表时,使用implode函数加入数组元素。因此,请按以下方式更改两个功能,

function my_followers_processor() {

    // post author ID
    $author_id  = $_POST['author_id'];
    $author_n   = $_POST['author_n'];

    // current user ID
    $current_id = $_POST['current_id'];

    // get current id followers meta
    $key        = 'followers';
    $single     = 'true';
    $followers  =   explode(",", get_user_meta($current_id, $key, $single));

    // append new follower to $current_f array
    $followers[] = $author_id;

    // update the users meta
    update_user_meta( $current_id, $key, implode(",", $followers));

    // redifrect when were done
    wp_redirect( site_url() . '/followers/?follow=' . $author_id);

    // end
    die();
}

function my_unfollowers_processor() {

    // post author ID
    $author_id  = $_POST['author_id'];
    $author_n   = $_POST['author_n'];

    // current user ID
    $current_id = $_POST['current_id'];

    // get current id followers meta
    $key        = 'followers';
    $single     = 'true';
    $followers  =   explode(",", get_user_meta($current_id, $key, $single));

    // remove follower
    if(($k = array_search($author_id, $followers)) !== false) {
        unset($followers[$k]);
    }

    // update user meta
    update_user_meta( $current_id, $key, implode(",", $followers));

    // redifrect when were done
    wp_redirect( site_url() . '/followers/?unfollow=' . $author_id);

    // end
    die();
}