rarebit Posted July 15, 2008 Share Posted July 15, 2008 Hi, I have a section which if your that way inclined, you might just try to put one massive word (all same letter) in. So i'm just looking for ideas of how to catch it. I've thought of a regex '/ [a-z]{11+} /', but i'd rather not use regex, so... The string could be exploded on the space, then check for the longest word... ? ? ? EDIT: just for fun: http://en.wikipedia.org/wiki/Longest_word_in_English http://wiki.answers.com/Q/What_is_the_longest_word_in_the_world Quote Link to comment https://forums.phpfreaks.com/topic/114827-the-longest-word-in-the-world/ Share on other sites More sharing options...
MadTechie Posted July 15, 2008 Share Posted July 15, 2008 what ? Quote Link to comment https://forums.phpfreaks.com/topic/114827-the-longest-word-in-the-world/#findComment-590430 Share on other sites More sharing options...
samshel Posted July 15, 2008 Share Posted July 15, 2008 what is the problem with using regex? Quote Link to comment https://forums.phpfreaks.com/topic/114827-the-longest-word-in-the-world/#findComment-590432 Share on other sites More sharing options...
rarebit Posted July 15, 2008 Author Share Posted July 15, 2008 MadTechie Say I have textarea which is entitled, 'Tell us a bit about yourself' and in this box old joe puts 'testttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttt', well I intend to reject it. samshel regex takes time to compile a search query and then to run it, so i'm wondering if it'd be quicker to do it myself, with an explode or my own char by char search (for a quick exit) Quote Link to comment https://forums.phpfreaks.com/topic/114827-the-longest-word-in-the-world/#findComment-590442 Share on other sites More sharing options...
MadTechie Posted July 15, 2008 Share Posted July 15, 2008 regex takes time to compile a search query and then to run it, so i'm wondering if it'd be quicker to do it myself, with an explode or my own char by char search (for a quick exit) NA, regex would be faster.. oh you doing this in a database query ? infact never mind Quote Link to comment https://forums.phpfreaks.com/topic/114827-the-longest-word-in-the-world/#findComment-590445 Share on other sites More sharing options...
samshel Posted July 15, 2008 Share Posted July 15, 2008 anyways regex would be safe and fast... Quote Link to comment https://forums.phpfreaks.com/topic/114827-the-longest-word-in-the-world/#findComment-590446 Share on other sites More sharing options...
rarebit Posted July 15, 2008 Author Share Posted July 15, 2008 yep, I love wasting time... function maxlen($s, $max) { $a = explode(" ", $s); foreach($a as $e) { if(strlen($e)>$max) { return 0; } } return 1; } function maxlen2($s, $max) { $n = 0; $len = strlen($s); for($i=0;$i<$len;$i++) { $n++; if($s[$i]==' ') { if($n>$max) { return 0; } $n = 0; } } return 1; } $tests = array(); $tests[] = "testtttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttt"; $tests[] = "test tttttttttttttttttttttttttt tttttttttttttttttt ttttttttttttttttt ttttt"; $tests[] = "test"; $tests[] = "test test test test test test test test test test test test abcdefghijklm"; $tests[] = "test abcdefghij abcdefghij abcdefghij abcdefghij abcdefghij abcdefghijklm"; $tests[] = "testtttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttt"; $tests[] = "test tttttttttttttttttttttttttt tttttttttttttttttt ttttttttttttttttt ttttt"; $tests[] = "test"; $tests[] = "test test test test test test test test test test test test abcdefghijklm"; $tests[] = "test abcdefghij abcdefghij abcdefghij abcdefghij abcdefghij abcdefghijklm"; $tests[] = "testtttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttt"; $tests[] = "test tttttttttttttttttttttttttt tttttttttttttttttt ttttttttttttttttt ttttt"; $tests[] = "test"; $tests[] = "test test test test test test test test test test test test abcdefghijklm"; $tests[] = "test abcdefghij abcdefghij abcdefghij abcdefghij abcdefghij abcdefghijklm"; $tests[] = "testtttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttt"; $tests[] = "test tttttttttttttttttttttttttt tttttttttttttttttt ttttttttttttttttt ttttt"; $tests[] = "test"; $tests[] = "test test test test test test test test test test test test abcdefghijklm"; $tests[] = "test abcdefghij abcdefghij abcdefghij abcdefghij abcdefghij abcdefghijklm"; $tests[] = "testtttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttt"; $tests[] = "test tttttttttttttttttttttttttt tttttttttttttttttt ttttttttttttttttt ttttt"; $tests[] = "test"; $tests[] = "test test test test test test test test test test test test abcdef0ghijklm"; $tests[] = "test abcdefghij abcdefghij abcdefghij abcdefghij abcdefghij abcdef#ghijklm"; $tests[] = "test abcdefghij abcdefghij abcdefghij abcdefghij abcdefghij abcdef_ghijklm"; $tests[] = "test abcdefghij abcdefghij abcdefghij abcdefghij abcdefghij abcdef-ghijklm"; $tests[] = "test abcdefghij abcdefghij abcdefghij abcdefghij abcdefghij abcdef=ghijklm"; $tests[] = "test abcdefghij abcdefghij abcdefghij abcdefghij abcdefghij abcdef@ghijklm"; $tests[] = "test abcdefghij abcdefghij abcdefghij abcdefghij abcdefghij abcdef'ghijklm"; $tests[] = "test abcdefghij abcdefghij abcdefghij abcdefghij abcdefghij abcdef:ghijklm"; $tests[] = "test abcdefghij abcdefghij abcdefghij abcdefghij abcdefghij abcdef!ghijklm"; $tests[] = "test abcdefghij abcdefghij abcdefghij abcdefghij abcdefghij abcdef\"ghijklm"; $tests[] = "test abcdefghij abcdefghij abcdefghij abcdefghij abcdefghij abcdef£ghijklm"; $tests[] = "test abcdefghij abcdefghij abcdefghij abcdefghij abcdefghij abcdef\$ghijklm"; $tests[] = "test abcdefghij abcdefghij abcdefghij abcdefghij abcdefghij abcdef%ghijklm"; $tests[] = "test abcdefghij abcdefghij abcdefghij abcdefghij abcdefghij abcdef^ghijklm"; $n = 0; $start = microtime(); foreach($tests as $e) { //if(preg_match('/[a-z]{11,}/i', $e)) if(preg_match('/[\w#_=\-@\':!"£\$%^]{11,}/i', $e)) // [a-zA-Z_0-9] { $n++; } } $end = microtime(); $len = $end - $start; print "Test duration: ".$len."<br>"; print "Total matches: ".$n."<br><br>"; $n = 0; $start = microtime(); foreach($tests as $e) { //if(preg_match('/[a-z]{11,}/i', $e)) if(preg_match('/[\S]{11,}/i', $e)) // [^ \f\n\r\t\v] { $n++; } } $end = microtime(); $len = $end - $start; print "Test duration: ".$len."<br>"; print "Total matches: ".$n."<br><br>"; $n = 0; $start = microtime(); foreach($tests as $e) { if(maxlen($e, 11)==0) { $n++; } } $end = microtime(); $len = $end - $start; print "Test duration: ".$len."<br>"; print "Total matches: ".$n."<br><br>"; $n = 0; $start = microtime(); foreach($tests as $e) { if(maxlen2($e, 11)==1) { $n++; } } $end = microtime(); $len = $end - $start; print "Test duration: ".$len."<br>"; print "Total matches: ".$n."<br><br>"; With the results: Test duration: 0.000382 Total matches: 32 Test duration: 0.000341 Total matches: 32 Test duration: 0.000398 Total matches: 32 Test duration: 0.002028 Total matches: 32 Quote Link to comment https://forums.phpfreaks.com/topic/114827-the-longest-word-in-the-world/#findComment-590481 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.