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?
To remove white spaces, the first though is to use
trim(), which you could either do in the loop for each tagor before:
In both cases, you will end up having to filter out the empty items in
$tagsSo it would possibly just be better to use preg_replace
Which would ensure that multiple white spaces are converted into a single space before exploding.