Php str replace删除所有逗号

Php str replace删除所有逗号,php,string,str-replace,codeigniter-3,Php,String,Str Replace,Codeigniter 3,所以我这里有一个非常简单的问题 当我在逗号分隔的列表上运行str_replace函数以删除前面带有逗号的值时,该函数会删除列表中的所有逗号 我做错了什么 有关对象: $tags=“16、17、18、20、21、22” $tag_id=“17” 守则: if (strpos($tags, ', '.$tag_id)) { //remove this in this format $new_tags = str_replace(', '.$tag_id, "", $tags); } elsei

所以我这里有一个非常简单的问题

当我在逗号分隔的列表上运行str_replace函数以删除前面带有逗号的值时,该函数会删除列表中的所有逗号

我做错了什么

有关对象:

$tags=“16、17、18、20、21、22”

$tag_id=“17”

守则:

if (strpos($tags, ', '.$tag_id))
{
 //remove this in this format
  $new_tags = str_replace(', '.$tag_id, "", $tags);
}
elseif (strpos($tags, $tag_id.', '))
{
  //remove this in this format
  $new_tags = str_replace($tag_id.', ', "", $tags);
}
else
{
  //just remove the number
  $new_tags = str_replace($tag_id, "", $tags);
}

我想你真正想要的是:

$tags = (...);
$tag_id = 17;
$tags_array = explode(',', $tags);
if(($idx = array_search($tag_id , $tags_array )) !== false) {
    unset($tags_array[$idx]);
}
$tags_cleaned = implode(', ', $tags_array);
//16, 18, 20, 21, 22

我想你真正想要的是:

$tags = (...);
$tag_id = 17;
$tags_array = explode(',', $tags);
if(($idx = array_search($tag_id , $tags_array )) !== false) {
    unset($tags_array[$idx]);
}
$tags_cleaned = implode(', ', $tags_array);
//16, 18, 20, 21, 22

我想你真正想要的是:

$tags = (...);
$tag_id = 17;
$tags_array = explode(',', $tags);
if(($idx = array_search($tag_id , $tags_array )) !== false) {
    unset($tags_array[$idx]);
}
$tags_cleaned = implode(', ', $tags_array);
//16, 18, 20, 21, 22

我想你真正想要的是:

$tags = (...);
$tag_id = 17;
$tags_array = explode(',', $tags);
if(($idx = array_search($tag_id , $tags_array )) !== false) {
    unset($tags_array[$idx]);
}
$tags_cleaned = implode(', ', $tags_array);
//16, 18, 20, 21, 22

在执行str\u替换之前,您的$tag\u id是否正确初始化?

在执行str\u替换之前,您的$tag\u id是否正确初始化?

在执行str\u替换之前,您的$tag\u id是否正确初始化?

我想,在数组中处理此csv列表操作更容易。使用explode和一些数组操作可以帮助您完成此操作

$list = array_map('trim', explode(',', $tags));
$flippedList = array_flip($list);
unset($flippedList[$tagId]);

$newTags = join(',', array_flip($flippedList));

我认为,在数组中处理csv列表操作更容易。使用explode和一些数组操作可以帮助您完成此操作

$list = array_map('trim', explode(',', $tags));
$flippedList = array_flip($list);
unset($flippedList[$tagId]);

$newTags = join(',', array_flip($flippedList));

我认为,在数组中处理csv列表操作更容易。使用explode和一些数组操作可以帮助您完成此操作

$list = array_map('trim', explode(',', $tags));
$flippedList = array_flip($list);
unset($flippedList[$tagId]);

$newTags = join(',', array_flip($flippedList));

我认为,在数组中处理csv列表操作更容易。使用explode和一些数组操作可以帮助您完成此操作

$list = array_map('trim', explode(',', $tags));
$flippedList = array_flip($list);
unset($flippedList[$tagId]);

$newTags = join(',', array_flip($flippedList));

这就是
str\u replace
的目的。替换字符串中要替换的内容的所有实例。但标记\u id是唯一的。或者这在这里真的无关紧要吗?您期望的输出是什么?将其张贴在这里以达到
str\u replace
的目的。替换字符串中要替换的内容的所有实例。但标记\u id是唯一的。或者这在这里真的无关紧要吗?您期望的输出是什么?将其张贴在这里以达到
str\u replace
的目的。替换字符串中要替换的内容的所有实例。但标记\u id是唯一的。或者这在这里真的无关紧要吗?您期望的输出是什么?将其张贴在这里以达到
str\u replace
的目的。替换字符串中要替换的内容的所有实例。但标记\u id是唯一的。或者这在这里真的不重要吗?你期望的结果是什么?也把它贴在这里