<?php

   $first="rahul";
  
   $second="shiva";

   for($i=0;$i<strlen($first);$i++)
       {
          for($j=0;$j<strlen($second);$j++)
             {
                 if($first[$i]==$second[$j])
                  {
                        
                    $first= "".substr($first,0,$i).substr($first,$i+1,strlen($first));

                        $second= "".substr($second,0,$j).substr($second,$j+1,strlen($second));
                      
                        $i--;
                                           
                        $j--; 
                     
                   }


              }
              

        }  
$len= Strlen($first) + strlen($second);
        $f="friends";
         $duplen=1;
  for($i=0;(strlen($f)!=1);)
  {


                if($len!=$duplen)
            {

                      if($i==(strlen($f)))
                    {


                         $i=0;



                     }

                     else
                     {
                         $i++;
                    }
                     $duplen++;           
}

else
      {

           $f=substr($f,0,$i).substr($f,$i+1,strlen($f)-1);

     }



   }


   
?>

i am not finding any mistake in the above code but its not getting executed.
its giving an error
"Fatal error: Maximum execution time of 30 seconds exceeded in C:\wamp\www\check.php on line 59 "
i hope i will be helped pls

I'm not really sure what you're trying to do, but perhaps you want to add a break after that line (now line 60, I guess?).

yes i got struck at that 60 th line
can i know why that's happening....
please let me know

can i know the reason please :?:

It's just a quick guess, that php's iterating over that line over and over again, try to add break; after that line. If this doesn't help, what is it that you're trying to do?

Member Avatar for diafol

Readable:

$first="rahul";
$second="shiva";
 
for($i=0;$i<strlen($first);$i++){
	for($j=0;$j<strlen($second);$j++){
		if($first[$i]==$second[$j]){
			$first= "".substr($first,0,$i).substr($first,$i+1,strlen($first));
			$second= "".substr($second,0,$j).substr($second,$j+1,strlen($second));
			$i--;
			$j--; 
		}
	}
}  
$len= Strlen($first) + strlen($second);
$f="friends";
$duplen=1;
for($i=0;(strlen($f)!=1);){
	if($len!=$duplen){
		if($i==(strlen($f))){
			$i=0;
		}else{
			$i++;
		}
		$duplen++;           
	}else{
		$f=substr($f,0,$i).substr($f,$i+1,strlen($f)-1);
	}
}

Looking at that, your loops look really suspect.

suspect means??
i didnt get u sir

usage of break didnt help me sir.
the concept is
i am havng two names out of them i have to remove the common lettters and calculate the length of the remaining words.
this length is stored in the variable len
and using this len variable i am counting the word 'friends'
for example
if length is 4
letter e of the word friends is removed and the new word obtained wil be jst 'frinds'
in the same way my code has to work till only single letter of the word friends wil remain..
this is the concept i wish to implement.
please help me in this

Member Avatar for diafol

THat's totally confused me, sorry.

So you have 2 strings and want to calculate their length if you remove the letters that are in both?

Rahul this FRIENDS game we were doing in school :)

Here i have made a script.
I think this is exactly what you want.
Try and give me feedback.

<?
	$first="rahul";
	$second="shiva";
	echo "<h2>$first & $second</h2>";
   	$len= strlen($first) + strlen($second);
 
	for($i=0;$i<strlen($first);$i++){
		for($j=0;$j<strlen($second);$j++){
			if($first[$i]==$second[$j])
			{
				echo '<br />Letter common : '.$first[$i];
				$len=$len-2;
			}
		}
	}	
	$frd = explode(" ","F R I E N D S");
	echo '<br />Remaining Length : '.$len;
	echo '<br /><br />';
	$cnt = 0;
	while(count($frd)!=1)
	{
		if($len>count($frd))
		{
			$de = ($len % (count($frd)));
			if($de == 0)
				$de = count($frd);
			$l = $de -1;
		}
		else
			$l = $len-1;
		
		echo '<br />'.implode('',$frd).' Cross on '.$frd[$l];
		unset($frd[$l]);
		
		$frd = explode('#',implode('#',$frd));
		echo ', Remaining '.implode('',$frd);	
		$cnt++;		
	}
	echo '<h2>Answer is : '.implode('',$frd).'</h2>';
	
?>
Member Avatar for diafol

If you just want to get the common characters:

$ans = array_intersect(array_unique(str_split($string1,1)),array_unique(str_split($string2,1)));

Or for multibyte chars:

$ans = array_intersect(array_unique(preg_split('/(?<!^)(?!$)/u', $string1 )),array_unique(preg_split('/(?<!^)(?!$)/u', $string2 )));

But I suspect you need something a little more complicated?

If you need no. characters not used:

$strlen1 = mb_strlen($string1,'UTF-8') - count($ans);

I'm not sure about the output you need:

echo "Common chars = " . implode("",$ans);

yes.
its one part.
and later i have to use that length to remove letters from the word "friends"

Have you tried this ????

Here i have made a script.
I think this is exactly what you want.
Try and give me feedback.

<?
	$first="rahul";
	$second="shiva";
	echo "<h2>$first & $second</h2>";
   	$len= strlen($first) + strlen($second);
 
	for($i=0;$i<strlen($first);$i++){
		for($j=0;$j<strlen($second);$j++){
			if($first[$i]==$second[$j])
			{
				echo '<br />Letter common : '.$first[$i];
				$len=$len-2;
			}
		}
	}	
	$frd = explode(" ","F R I E N D S");
	echo '<br />Remaining Length : '.$len;
	echo '<br /><br />';
	$cnt = 0;
	while(count($frd)!=1)
	{
		if($len>count($frd))
		{
			$de = ($len % (count($frd)));
			if($de == 0)
				$de = count($frd);
			$l = $de -1;
		}
		else
			$l = $len-1;
		
		echo '<br />'.implode('',$frd).' Cross on '.$frd[$l];
		unset($frd[$l]);
		
		$frd = explode('#',implode('#',$frd));
		echo ', Remaining '.implode('',$frd);	
		$cnt++;		
	}
	echo '<h2>Answer is : '.implode('',$frd).'</h2>';
	
?>

i have less knowledge in php
i am not understanding the functions u have used in the program
please can i know them

Can you create test.php and copy all my above code and run it in your browser.
And post whether your output is correct or not??

yes i did
its wrong answer
its displaying the text u wrote

Your php shortag setting is closed.
Replace <? to <?php at first line.

yes sir its right
can i know the site where i can find out those functions please
i have no knowledge about those functions
and
what's the wrong with my code?

Find my inline comments.
Your logic and loop was messy and i didn't get it.
So i have recreated code.
For any php function help visit http://php.net/

<?
	$first="rahul";
	$second="shiva";
	echo "<h2>$first & $second</h2>";
   	$len= strlen($first) + strlen($second); // total no of letter in both words
 	
	// in this for loop we will check for matching  letters
	for($i=0;$i<strlen($first);$i++){
		for($j=0;$j<strlen($second);$j++){
			if($first[$i]==$second[$j])
			{
				echo '<br />Letter common : '.$first[$i];
				$len=$len-2; // two letters found common so 2 letters removed from total count
			}
		}
	}	
	$frd = explode(" ","F R I E N D S"); // created array for FRIENDS iteration
	echo '<br />Remaining Length : '.$len;
	echo '<br /><br />';
	$cnt = 0;
	while(count($frd)!=1) // untill we get only one element from FRIENDS do the loop
	{
		// this if-else loop will check the location which will be cut from FRIENDS
		if($len>count($frd))
		{
			$de = ($len % (count($frd)));
			if($de == 0)
				$de = count($frd);
			$l = $de -1;
		}
		else
			$l = $len-1;
		
		echo '<br />'.implode('',$frd).' Cross on '.$frd[$l];
		unset($frd[$l]); // removing crossed/cut element from FRIENDS array
		
		$frd = explode('#',implode('#',$frd));
		echo ', Remaining '.implode('',$frd);	
		$cnt++;		
	}
	echo '<h2>Answer is : '.implode('',$frd).'</h2>';
	
?>

It's a way to put an array of strings together into one string.

then explode is to split??
what's the use of # in explode ?

Erm, it's the string you want to delimit the substrings with (kinda) so

explode(' ', 'hello world here I come');

becomes

array('hello','world','here','I','come');

Anyway, I don't understand that line either. vibhadevit, what's the logic in line 37?

oh thanks sir for clearing my doubts

Member Avatar for diafol

I had a few minutes on my hands:

//====================================================================//
//======================== commonChars Function ======================//
//====================================================================//

//  last optional parameter ($i) can take two values:
//  1 = compare lowercase versions of strings
//  2 = compare uppercase versions of strings
//  otherwise comparison is case-sensitive

function commonChars($string1,$string2,$i=0){
	if($i == 1){ 		//change to lowercase
		$string1 = mb_strtolower($string1,'UTF-8');
		$string2 = mb_strtolower($string2,'UTF-8');
	}elseif($i == 2){	//change to uppercase
		$string1 = mb_strtoupper($string1,'UTF-8');
		$string2 = mb_strtoupper($string2,'UTF-8');
	}
	$r_string1 = array_unique(preg_split('/(?<!^)(?!$)/u', $string1 ));
	$r_string2 = array_unique(preg_split('/(?<!^)(?!$)/u', $string2 ));
	$i_string1 = count($r_string1);
	$i_string2 = count($r_string2);
	
	$output = 	"<h3>Comparing $string1 and $string2 &hellip;</h3>";
	
	$common = array_intersect(array_unique($r_string1),array_unique($r_string2));
	$i_common = count($common);
	if($i_common == 0){
		$output .= "<p>No common characters found for $string1 and $string2.</p>";
	}else{
		$out_string1 = $i_string1 - $i_common;
		$out_string2 = $i_string2 - $i_common;
		$r_common = implode(",",$common);
		$pluralc = ($i_common > 1) ? 's' : '';
		$plural1 = ($i_string1 > 1) ? 's' : '';
		$plural2 = ($i_string2 > 1) ? 's' : '';
		
		$output .= 	"<p>For $string1: $i_common character$pluralc from $i_string1 unique character$plural1.</p>" .
				"<p>For $string2: $i_common character$pluralc from $i_string2 unique character$plural2.</p>" .
				"<p>The common characters were $r_common</p>";
	}
	return $output;
}



$str1 = "Österreich";
$str2 = "öocyte";
//Note the use of multibyte characters

//====================================================================//
//====================== sample usage of function ====================//
//====================================================================//

echo commonChars($str1,$str2);		//case-sensitive

echo commonChars($str1,$str2,1);	//compare using lowercase conversion

echo commonChars($str1,$str2,2);	//compare using uppercase conversion
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.