我有以下代码
//check out the tags, if it allready exist update with 1 if not create new record
$tag_item = "";
$tags = explode(" ", $tags);
foreach($tags as $tag):
if(!empty($tag) && $tag != " "):
$t_sql = mysqli_query($link, "SELECT id, times FROM shop_tags WHERE tag='".$tag."'");
if(mysqli_num_rows($t_sql) == 0):
mysqli_query($link, "INSERT INTO shop_tags (tag, times) VALUES ('".$tag."', 1)");
//find last updated id of the tags
$lastid = mysqli_insert_id($link);
$tag_item .= $lastid." ";
endif;
endif;
endforeach;
所以我使用explode来分隔每个标签,但是如果用户不小心添加到空格怎么办?我应该先使用 preg_match 来过滤吗?如果有的话,我如何删除最后一个空格?