silvercover Posted July 22, 2010 Share Posted July 22, 2010 Hi, How can I make sure that the entered list is comma separated correctly? It must be a,b,c,... and not a,,b,c,-,d with other wrong separators or some thing similar. the right format for me is String1,String2,String3,... Thanks in Advance. Quote Link to comment https://forums.phpfreaks.com/topic/208540-make-sure-the-list-is-comma-seperated/ Share on other sites More sharing options...
DaiLaughing Posted July 22, 2010 Share Posted July 22, 2010 Where does the list come from? Will the strings have spaces in? Or commas? Quote Link to comment https://forums.phpfreaks.com/topic/208540-make-sure-the-list-is-comma-seperated/#findComment-1089568 Share on other sites More sharing options...
silvercover Posted July 22, 2010 Author Share Posted July 22, 2010 I have an input field for entering comma separated tags and I want to make sure I get the list correctly. Thanks. Quote Link to comment https://forums.phpfreaks.com/topic/208540-make-sure-the-list-is-comma-seperated/#findComment-1089569 Share on other sites More sharing options...
DaiLaughing Posted July 22, 2010 Share Posted July 22, 2010 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. Quote Link to comment https://forums.phpfreaks.com/topic/208540-make-sure-the-list-is-comma-seperated/#findComment-1089574 Share on other sites More sharing options...
timvdalen Posted July 22, 2010 Share Posted July 22, 2010 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,] Quote Link to comment https://forums.phpfreaks.com/topic/208540-make-sure-the-list-is-comma-seperated/#findComment-1089575 Share on other sites More sharing options...
silvercover Posted July 22, 2010 Author Share Posted July 22, 2010 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? Quote Link to comment https://forums.phpfreaks.com/topic/208540-make-sure-the-list-is-comma-seperated/#findComment-1089577 Share on other sites More sharing options...
DaiLaughing Posted July 22, 2010 Share Posted July 22, 2010 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. Quote Link to comment https://forums.phpfreaks.com/topic/208540-make-sure-the-list-is-comma-seperated/#findComment-1089581 Share on other sites More sharing options...
silvercover Posted July 22, 2010 Author Share Posted July 22, 2010 DaiLaughing, since I have no time for playing with words then Thanks for your try. I think I'll stick to timvdalen's solution. Quote Link to comment https://forums.phpfreaks.com/topic/208540-make-sure-the-list-is-comma-seperated/#findComment-1089584 Share on other sites More sharing options...
timvdalen Posted July 22, 2010 Share Posted July 22, 2010 Just doing a replace for -, ,/ and _ to ',' isn't that hard. Then do a regex match to keep only [a-zA-z0-9,] Echo, echo, echo Quote Link to comment https://forums.phpfreaks.com/topic/208540-make-sure-the-list-is-comma-seperated/#findComment-1089585 Share on other sites More sharing options...
DaiLaughing Posted July 22, 2010 Share Posted July 22, 2010 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. Quote Link to comment https://forums.phpfreaks.com/topic/208540-make-sure-the-list-is-comma-seperated/#findComment-1089630 Share on other sites More sharing options...
timvdalen Posted July 23, 2010 Share Posted July 23, 2010 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. Quote Link to comment https://forums.phpfreaks.com/topic/208540-make-sure-the-list-is-comma-seperated/#findComment-1090013 Share on other sites More sharing options...
Zane Posted July 23, 2010 Share Posted July 23, 2010 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) ); Quote Link to comment https://forums.phpfreaks.com/topic/208540-make-sure-the-list-is-comma-seperated/#findComment-1090018 Share on other sites More sharing options...
timvdalen Posted July 23, 2010 Share Posted July 23, 2010 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 Quote Link to comment https://forums.phpfreaks.com/topic/208540-make-sure-the-list-is-comma-seperated/#findComment-1090022 Share on other sites More sharing options...
Zane Posted July 23, 2010 Share Posted July 23, 2010 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. Quote Link to comment https://forums.phpfreaks.com/topic/208540-make-sure-the-list-is-comma-seperated/#findComment-1090023 Share on other sites More sharing options...
DaiLaughing Posted July 23, 2010 Share Posted July 23, 2010 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! Quote Link to comment https://forums.phpfreaks.com/topic/208540-make-sure-the-list-is-comma-seperated/#findComment-1090042 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.