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

  • SEARCH
  • Home
  • 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 501285
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T06:10:26+00:00 2026-05-13T06:10:26+00:00

I have a php variable that comes from a form that needs tidying up.

  • 0

I have a php variable that comes from a form that needs tidying up. I hope you can help.

The variable contains a list of items (possibly two or three word items with a space in between words).
I want to convert it to a comma separated list with no superfluous white space. I want the divisions to fall only at commas, semi-colons or new-lines. Blank cannot be an item.

Here’s a comprehensive example (with a deliberately messy input):

Variable In: "dog, cat         ,car,tea pot,,  ,,, ;;(++NEW LINE++)fly,     cake"
Variable Out "dog,cat,car,tea pot,fly,cake"

Can anyone help?

  • 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-13T06:10:27+00:00Added an answer on May 13, 2026 at 6:10 am

    You can start by splitting the string into “useful” parts, with preg_split, and, then, implode those parts back together :

    $str_in = "dog, cat         ,car,tea pot,,  ,,, ;;
    fly,     cake";
    
    $parts = preg_split('/[,;\s]/', $str_in, -1, PREG_SPLIT_NO_EMPTY);
    
    $str_out = implode(',', $parts);
    
    var_dump($parts, $str_out);
    

    (Here, the regex will split on ‘,‘, ‘;‘, and ‘\s‘, which means any whitespace character — and we only keep non-empty parts)

    Will get you, for $parts :

    array
      0 => string 'dog' (length=3)
      1 => string 'cat' (length=3)
      2 => string 'car' (length=3)
      3 => string 'tea' (length=3)
      4 => string 'pot' (length=3)
      5 => string 'fly' (length=3)
      6 => string 'cake' (length=4)
    

    And, for $str_out :

    string 'dog,cat,car,tea,pot,fly,cake' (length=28)
    


    Edit after the comment : sorry, I didn’t notice that one ^^

    In that case, you can’t split by white-space 🙁 I would probably split by ‘,‘ or ‘;‘, iterate over the parts, using trim to remove white-characters at the beginning and end of each item, and only keep those that are not empty :

    $useful_parts = array();
    $parts = preg_split('/[,;]/', $str_in, -1, PREG_SPLIT_NO_EMPTY);
    foreach ($parts as $part) {
        $part = trim($part);
        if (!empty($part)) {
            $useful_parts[] = $part;
        }
    }
    var_dump($useful_parts);
    

    Executing this portion of code gets me :

    array
      0 => string 'dog' (length=3)
      1 => string 'cat' (length=3)
      2 => string 'car' (length=3)
      3 => string 'tea pot' (length=7)
      4 => string 'fly' (length=3)
      5 => string 'cake' (length=4)
    

    And imploding all together, I get, this time :

    string 'dog,cat,car,tea pot,fly,cake' (length=28)
    

    Which is better 😉

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

Sidebar

Ask A Question

Stats

  • Questions 488k
  • Answers 488k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Yes that is possible. $(…).change(function () { /* fn1 */… May 16, 2026 at 8:34 am
  • Editorial Team
    Editorial Team added an answer String.split(":") -> array of "07", "00", "00" parseInt on each… May 16, 2026 at 8:34 am
  • Editorial Team
    Editorial Team added an answer VC has a __COUNTER__ macro which gives uniqueness, it appears… May 16, 2026 at 8:34 am

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Related Questions

No related questions found

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.