RE: How I can find longest common prefix of strings

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Try something like this.
	//set up
	$str1 = explode($array1);
	$str2 = explode($array2);
	$count1 = count($str1);
	$count2 = count($str2);

	if(  ($count1=='0') ||  ($count2 =='0') ) 
	{
		endComparison(); 
	}

	// find out which array is longer, then loop through the arrays and
end when a mismatch is found
	elseif ( $count1 > =  $count2)
	{
		for($i='0'; $i<$count2;$i++)	
		{
		$holder[$i] = ( $str1[$i]==$str2[$i] ) ? $str1[$i] :
endComparison(); 
		}

	}
	else {
		for($i='0'; $i<$count1;$i++)	
		{
		$holder[$i] = ( $str2[$i]==$str1[$i] ) ? $str1[$i] :
endComparison(); 
		}

	}

	//stitch it back together

	$prefix = implode('',$holder);

What about coverting the strings to arrays, then comparing each identical
value. If the alues match, then add it to a holder array. If not, end.

-----Original Message-----
From: php-objects@xxxxxxxxxxxxxxx [mailto:php-objects@xxxxxxxxxxxxxxx] On
Behalf Of ayk_amanov
Sent: Monday, June 26, 2006 1:37 AM
To: php-objects@xxxxxxxxxxxxxxx
Subject:  How I can find longest common prefix of strings

Hi, progs!!!

I'm interested in such algorithm that solves problem of finding longest
common prefix of two or more strings.

I haven't found such function in PHP, or I pass it???
Can you help me with this problem???

Thanks,
Aybek










PHP Data object relational mapping generator
http://www.metastorage.net/ 
Yahoo! Groups Links



 








------------------------ Yahoo! Groups Sponsor --------------------~--> 
See what's inside the new Yahoo! Groups email.
http://us.click.yahoo.com/2pRQfA/bOaOAA/yQLSAA/saFolB/TM
--------------------------------------------------------------------~-> 

PHP Data object relational mapping generator
http://www.metastorage.net/ 
Yahoo! Groups Links

<*> To visit your group on the web, go to:
    http://groups.yahoo.com/group/php-objects/

<*> To unsubscribe from this group, send an email to:
    php-objects-unsubscribe@xxxxxxxxxxxxxxx

<*> Your use of Yahoo! Groups is subject to:
    http://docs.yahoo.com/info/terms/
 




[Index of Archives]     [PHP Home]     [PHP Users]     [PHP Soap]     [Kernel Newbies]     [Yosemite]     [Yosemite Campsites]

  Powered by Linux