Jump to content

Recommended Posts

Although I have worked with php for some time now, I am really stuck with this problem. I have 2 strings each starting as the same but ending differently. The two strings are totally random so how do I remove the unique endings of the 2 strings to end up with a variable containing the common beginning. Below is an example of two strings and on the fourth line is what the result should be although the computer needs to work out the common beginning.

$var1="This is testing";
$var2="This is tests";
//below needs to somehow be generated.
$result_should_equal="This is test";

 

Just to give you an idea of what I mean by random below is another example

$var1="another_string";
$var2="another example";
//below needs to somehow be generated.
$result_should_equal="another";

 

Does anybody have any ideas on how to do this as I don't have a clue.

Link to comment
https://forums.phpfreaks.com/topic/136340-solved-filter-random-string/
Share on other sites

Erm, i'd loop through the strings copying across to an array until the characters aren't equal. Something like:

 

<?php

function find_common_start($str1,$str2){
    $common = array();
    $length = (strlen($str1) >= strlen($str2)) ? strlen($str1) : strlen($str2);
    for($x=0;$x<$length;$x++){
        if($str1[$x] == $str2[$x]){
            $common[] = $str1[$x]; 
        }else{
            break;
        }
    }
    return join('',$common);
}

$common = ("Test","Testing");
?>

<?PHP
function StringMatch($str1, $str2) {
	$similarity = '';
	for ($i=0; $i<strlen($str1); $i++) {
		if (substr($str1, $i, 1) == substr($str2, $i, 1)) {
			$similarity .= substr($str1, $i, 1);
		}
		else
			return $similarity;
	}
	return $similarity;
}
echo "The string found in both strings : '".StringMatch('string value one', 'string value two')."'";
?>

Erm, i'd loop through the strings copying across to an array until the characters aren't equal. Something like:

 

<?php

function find_common_start($str1,$str2){
    $common = array();
    $length = (strlen($str1) >= strlen($str2)) ? strlen($str1) : strlen($str2);
    for($x=0;$x<$length;$x++){
        if($str1[$x] == $str2[$x]){
            $common[] = $str1[$x]; 
        }else{
            break;
        }
    }
    return join('',$common);
}

$common = ("Test","Testing");
?>

 

Although that code may use a little more cpu than the below code, it seems to work with 100% percision. So the code in the quote above is what I am using and thanks for solving what seemed to be the near impossible. Just a note code the code below. I discovered if you use long numbers and maybe even sentences, it will mix up some characters. That is why I decided it to be neccessary to use the example above as it checks each character as described. So the below example will not work just as a reminder although it was a wonderful try and would have used less cpu than the above example if it did work. Thanks for the fast replies everybody.

<?php

$var1 = '1415926535897932384626433832795028841971693993751058209749445923078164062862089986280348253421170679821480865132823066470938446095505822317253594081284811174502841027019385211055596446229489549303819644288109756659334461284756482337867831652712019091456485669234603486104543266482133936072602491412737245870066063155881748815209209628292540917153643678925903600113305305488204665213841469519415116094330572703657595919530921861173819326117829803590550576595899588575050360965089112212605040644547741702891497593219945980055780404580419998765233121526278650209604041392589197182197102028353402197967330617747762130174406333877068240797363099530367751255951759392084575897930438538713656162192371525901378953736555235767587530134989147700187632601617253709427279969413198950392820810873325985513661725333050581462264152088358352253762662280390193227765597080428810746457108356411762980121391364183822116594192533978710075278614632612842997974221412598803665585346098144053269088458433189901366823234230';


$var2 = '1415926535897932384626433832795028841971693993751058209749445923078164062862089986280348253421170679821480865132823066470938446095505822317253594081284811174502841027019385211055596446229489549303819644288109756659334461284756482337867831652712019091456485669234603486104543266482133936072602491412737245870066063155881748815209209628292540917153643678925903600113305305488204665213841469519415116094330572703657595919530921861173819326117905739286748202616824617765517729384417714737360796533726553899475424616609911318159105563243069185220238546730526740323180054476969498121192160568226858070030322658697894122020307791862552130882762843684870778225001246607208294700581731524961294982645762323401909888104979776606603362791883485527270218935229017317660905117353287534933579835666319492499137820795946091280667850291908505375340750707862012790852681845130736477260926495602003587330496660284429581217052514475525377381905161628453031674290929592935896710397771127183462457443634980018913852492898';

$mergedVars = implode(array_intersect_assoc(str_split($var1),str_split($var2)));
echo $mergedVars;

?>

*solved*

It's the final implode that breaks $mergedVars = implode(array_intersect_assoc(str_split($var1),str_split($var2)));

 

It needs to merge only consecutive index values (starting from 0) back into the string, discarding anything including and after the first non-consecutive key value.

 

e.g.

array(0=> 'A', 1=> 'B', 2=> 'C', 26 => 'Z')

should merge the values for consecutive keys 0, 1 and 2, but discard 26.

 

Not sure if anybody can come up with a quick one-line to do that?

 

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.