I have the following code
//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;
So I use explode to seperate each tag, but what if a user by accident added to spaces? should I use preg_match first to filter for this? and how do I remove the last white space if there is any?