Jump to content

Recommended Posts

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

Link to comment
https://forums.phpfreaks.com/topic/114827-the-longest-word-in-the-world/
Share on other sites

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)

 

 

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

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

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.