jamiet757 Posted April 25, 2010 Share Posted April 25, 2010 I am trying to figure out how to tell if what a user enters in a textbox is at least 7 words, separated by commas. It can be 100 words, I don't care, as long as it is at least 7. It will appear like this: word1,word2,word3,word4,word5,word6,word7 or word1, word2, word3, word4, word5, word6, word7 (with spaces) Anyone have a statement that I can use? Coincidentally, I need to check to see if there is at least 7 words in a sentence as well, i.e.: The quick brown fox jumped over the. It could be 100 words again, but it needs to be a minimum of 7. Any help is appreciated! Quote Link to comment https://forums.phpfreaks.com/topic/199688-at-least-7-words-separated-by-commas/ Share on other sites More sharing options...
cags Posted April 25, 2010 Share Posted April 25, 2010 How accurate do you need to be, the simplest solution would probably be to use... substr_count( $string, ','); // or substr_count( $string, ' '); Quote Link to comment https://forums.phpfreaks.com/topic/199688-at-least-7-words-separated-by-commas/#findComment-1048077 Share on other sites More sharing options...
jamiet757 Posted April 25, 2010 Author Share Posted April 25, 2010 It is just to make sure someone has entered at least 7 keywords and a description of at least 7 words, to prevent things like "A photo." that give no meaning to the description. Quote Link to comment https://forums.phpfreaks.com/topic/199688-at-least-7-words-separated-by-commas/#findComment-1048078 Share on other sites More sharing options...
cags Posted April 25, 2010 Share Posted April 25, 2010 The previous code will work, but can be tricked with ' ' or ',,,,,,,,'. A better solution could be created with Regex if needed. Something like this to check for comma separated. '#([^,]+,){7,}#i' ...and for the spaces... '#([^ ]+ ){7,}#i' Quote Link to comment https://forums.phpfreaks.com/topic/199688-at-least-7-words-separated-by-commas/#findComment-1048081 Share on other sites More sharing options...
jamiet757 Posted April 25, 2010 Author Share Posted April 25, 2010 Great thanks! Quote Link to comment https://forums.phpfreaks.com/topic/199688-at-least-7-words-separated-by-commas/#findComment-1048083 Share on other sites More sharing options...
cags Posted April 25, 2010 Share Posted April 25, 2010 Just thought, that will require the pattern ending with a comma, try this instead... '#([^,]+,){7,}.*#i' ...still may not be perfect, haven't tested it or anything. Quote Link to comment https://forums.phpfreaks.com/topic/199688-at-least-7-words-separated-by-commas/#findComment-1048088 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.