Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 616683
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T18:21:11+00:00 2026-05-13T18:21:11+00:00

I have the following code //check out the tags, if it allready exist update

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T18:21:12+00:00Added an answer on May 13, 2026 at 6:21 pm

    To remove white spaces, the first though is to use trim(), which you could either do in the loop for each tag

    foreach($tags as $tag):
        $tag = trim($tag);
    

    or before:

    $tags = explode(" ", $tags);
    $tags = array_map('trim', $tags);
    

    In both cases, you will end up having to filter out the empty items in $tags

    foreach($tags as $tag):
        $tag = trim($tag); //This line is only required if you didn't use array_map
        if ( empty($tag){
            continue;
        }
    

    So it would possibly just be better to use preg_replace

    $tags = preg_replace('/\s\s+/',' ', $tags);
    $tags = explode(' ', $tags);
    

    Which would ensure that multiple white spaces are converted into a single space before exploding.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following PHP code // Check if the upload is setted if
I have the following piece of code to check for supported intents. The code
I have the following code: foreach (ListItem item in check.Items) { if (item.Selected) {
I have the following piece of code where check is for char type do{
I have the following code to check for form data and I can't figure
I use the following piece of code to check out and then check in
Suppose we have the following code. I want to swap out the inner parse_place
So I have the following code to check for the text file called Doctors.txt
I have following code if (rs != null) { out.println(result set has got something);
I have the following code, what is the easiest way to check if $data

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.