Jump to content

Make sure the list is comma seperated


silvercover

Recommended Posts

You didn't answer the other two questions.

 

Would having more than one field in the form help?  Then PHP could put the commas in with no chance of mistakes.

Just doing a replace for -, ,/ and _ to ',' isn't that hard. Then do a regex match to keep only [a-zA-z0-9,]

Link to comment
Share on other sites

You didn't answer the other two questions.

 

Would having more than one field in the form help?  Then PHP could put the commas in with no chance of mistakes.

 

You've asked:

Where does the list come from?

 

I said:

I have an input field for entering comma separated tags

 

You've asked:

Will the strings have spaces in?  Or commas?

 

I said:

the right format for me is String1,String2,String3,...

&

I have an input field for entering comma separated tags and I want to make sure I get the list correctly.

 

You've asked:

Would having more than one field in the form help?

 

I said:

I have an input field for entering comma separated tags and I want to make sure I get the list correctly.

 

So which question is unanswered?

Link to comment
Share on other sites

Bearing in mind I'm trying to help you might want to be a bit nicer in your tone.

 

To try to explain:

 

 

You've asked:

Will the strings have spaces in?  Or commas?

 

I said:

the right format for me is String1,String2,String3,...

 

That doesn't answer the question which was whether those strings could hold spaces or commas.

 

You've asked:

Would having more than one field in the form help?

 

I said:

I have an input field for entering comma separated tags and I want to make sure I get the list correctly.

 

 

Indeed, but I asked whether it "would" help.  Often giving a user a field for each item of data reduces the chance of mistakes.  I was trying to suggest that if you knew how many items of data there might be you could provide that many text fields.  As you didn't provide much information I have to guess.

 

Link to comment
Share on other sites

But timvdalen's answer only works depending on the answers to my original questions.  That's why I asked them.  I'm not playing with words I am asking vital questions so that I don't recommend a solution which might go wrong.  Obviously I should save my time and not answer your posts in the future - no problem.

Link to comment
Share on other sites

But timvdalen's answer only works depending on the answers to my original questions.  That's why I asked them.  I'm not playing with words I am asking vital questions so that I don't recommend a solution which might go wrong.  Obviously I should save my time and not answer your posts in the future - no problem.

Hey, he's new with PHP, go easy on him. I know you want to be respected for your skills and stuff, but he just wants to know the easiest way to do this.

Link to comment
Share on other sites

I'm not exactly sure what "format" you're looking for, but you can use PHP's explode() function to explode on commas.  Then you will have an array of everything..... that is separated by commas.

 

For instance

$inputField = "cats, dogs, birds, burds, turds, water, pall, mall, cigarettes";
$tags = explode(",", $inputField)

 

Then at which point you might want to "validate each tag... or trim of whitespace, or malicious characters

foreach($tags as $v=>$tag) $tags[$v] = mysql_real_escape_string( trim($tag) );

Link to comment
Share on other sites

I'm not exactly sure what "format" you're looking for, but you can use PHP's explode() function to explode on commas.  Then you will have an array of everything..... that is separated by commas.

 

For instance

$inputField = "cats, dogs, birds, burds, turds, water, pall, mall, cigarettes";
$tags = explode(",", $inputField)

 

Then at which point you might want to "validate each tag... or trim of whitespace, or malicious characters

foreach($tags as $v=>$tag) $tags[$v] = mysql_real_escape_string( trim($tag) );

I'm curious as to why you would first explode and then validate. If you convert dashes and slashes to commas first, you'd get way better results, imo

Link to comment
Share on other sites

If you convert dashes and slashes to commas first, you'd get way better results, imo

That makes no logical sense.  If you were to do that, you would be adding tags.  Or better yet, splitting the tags the user wanted.

 

Let's say I explicitly wanted a tag called "pot_roast"; If I replaced the underscore with a comma before exploding, then I would have 2 tags.

Link to comment
Share on other sites

Hey, he's new with PHP, go easy on him. I know you want to be respected for your skills and stuff, but he just wants to know the easiest way to do this.

 

It wasn't meant to sound sarcastic.  I'm happy to help or not - genuinely.  Apologies if it sounded nasty.  The ongoing discussion though shows that we don't have enough info to be sure that what is being suggested will work.  As for being respected for my skills and stuff believe me I'm here to learn as much as to help!

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.