phpnoobie9 Posted March 28, 2008 Share Posted March 28, 2008 Is this correct...it seems like I just keep getting my else statement when I insert in words into a form. $longtext should check for words that are longer that 20 characters without any space from the description which is the textarea of the form. $description is the textarea name on the form. $longtext = preg_match('/^\S{20,}$/', $description); if($description != $longtext) { if word is not longer than 20 characters do this else { if a word has more than 20 characters without any blank space do this } Quote Link to comment https://forums.phpfreaks.com/topic/98412-checking-for-word-length/ Share on other sites More sharing options...
BlueSkyIS Posted March 28, 2008 Share Posted March 28, 2008 i'd just use strlen() if(strlen(str_replace(" ","",$description)) <= 20) { //if word is not longer than 20 characters do this else { //if a word has more than 20 characters without any blank space do this } Quote Link to comment https://forums.phpfreaks.com/topic/98412-checking-for-word-length/#findComment-503615 Share on other sites More sharing options...
MasterACE14 Posted March 28, 2008 Share Posted March 28, 2008 i'd just use strlen() and use str_replace() for the spaces. EDIT: beat me to it Quote Link to comment https://forums.phpfreaks.com/topic/98412-checking-for-word-length/#findComment-503619 Share on other sites More sharing options...
BlueSkyIS Posted March 28, 2008 Share Posted March 28, 2008 haha, thought of it after i posted and updated my post. Quote Link to comment https://forums.phpfreaks.com/topic/98412-checking-for-word-length/#findComment-503620 Share on other sites More sharing options...
phpnoobie9 Posted March 28, 2008 Author Share Posted March 28, 2008 i'd just use strlen() So if I use strlen($description) won't that count every character in $description including space? My goal is to not let the form submit if a there was a word found that has more than 20 characters without a space. i'd just use strlen() and use str_replace() for the spaces. EDIT: beat me to it I don't want to replace the word.. I want to echo the else which will say something along the lines of error. Quote Link to comment https://forums.phpfreaks.com/topic/98412-checking-for-word-length/#findComment-503622 Share on other sites More sharing options...
MasterACE14 Posted March 28, 2008 Share Posted March 28, 2008 I think I get what you mean... maybe you should be breaking the word up into a array? Quote Link to comment https://forums.phpfreaks.com/topic/98412-checking-for-word-length/#findComment-503627 Share on other sites More sharing options...
phpnoobie9 Posted March 28, 2008 Author Share Posted March 28, 2008 So..how do I count the each word? Quote Link to comment https://forums.phpfreaks.com/topic/98412-checking-for-word-length/#findComment-503634 Share on other sites More sharing options...
BlueSkyIS Posted March 28, 2008 Share Posted March 28, 2008 you're not replacing anything except removing the spaces before measuring what's left over. $description = "this is a bit of text"; if(strlen(str_replace(" ","",$description)) <= 20) { //if word is not longer than 20 characters do this else { //if a word has more than 20 characters without any blank space do this } echo $description; output: this is a bit of text Quote Link to comment https://forums.phpfreaks.com/topic/98412-checking-for-word-length/#findComment-503635 Share on other sites More sharing options...
phpnoobie9 Posted March 28, 2008 Author Share Posted March 28, 2008 you're not replacing anything except removing the spaces before measuring what's left over. $description = "this is a bit of text"; if(strlen(str_replace(" ","",$description)) <= 20) { //if word is not longer than 20 characters do this else { //if a word has more than 20 characters without any blank space do this } echo $description; output: this is a bit of text So " " is equal to space what about ""? Isn't this telling space to replace $description? then...if $description is <= 20 do if.. Just need a little explaining so I'll understand the code. Thanks. Quote Link to comment https://forums.phpfreaks.com/topic/98412-checking-for-word-length/#findComment-503638 Share on other sites More sharing options...
phpnoobie9 Posted March 28, 2008 Author Share Posted March 28, 2008 I did tried the code BlueSkyIs posted, but it's counting everything in the description textarea. So if I have several words with a few characters..that adds up to 10 or more it'll echo else. Quote Link to comment https://forums.phpfreaks.com/topic/98412-checking-for-word-length/#findComment-503646 Share on other sites More sharing options...
KrisNz Posted March 29, 2008 Share Posted March 29, 2008 If I understand the problem correctly (you want to find, in a string, if theres at least one word longer than 20 characters),I think your original regex would work if you took out those anchors (the ^ and the $). <?php $desc = <<<EOF This is a test sentence with a supercalafragalisticexpialedocius word EOF; if (preg_match("/\S{20,}/",$desc)) { echo "match"; } else { echo "no words longer than 20 chars"; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/98412-checking-for-word-length/#findComment-503794 Share on other sites More sharing options...
sasa Posted March 29, 2008 Share Posted March 29, 2008 try $longtext = preg_match('/\S{20,}/', $description); if(!$longtext) { if word is not longer than 20 characters do this else { if a word has more than 20 characters without any blank space do this } Quote Link to comment https://forums.phpfreaks.com/topic/98412-checking-for-word-length/#findComment-503806 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.