【发布时间】:2015-07-09 05:10:06
【问题描述】:
所以我在这里有一个非常简单的问题。
当我在逗号分隔列表上运行 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);
}
【问题讨论】:
-
这就是
str_replace的目的。替换字符串中要替换的所有实例。 -
但 tag_id 是唯一的。或者这在这里真的不重要吗?
-
你期望的输出也在这里发布
标签: php string str-replace codeigniter-3