Jump to content

Replacing Wildcards


Drompo2

Recommended Posts

Been trying to solve this problem for a while now, but have got nowhere.

 

If a user enters a string that contains certian ambigious characters, eg. Y, M, D

It replaces them with non-ambigious characters, this being a simple preg_replace function. But each letter (Y,M,D) has multiple non-ambigious characters, therefore could have several different combinations.

 

Any help at all very appreciated.

 

Ashley

Link to comment
Share on other sites

Here we go.

 

This is without recursion, AKA

 

Y = AB

M = XY therefor, M = XAB

 

Will not work. It can be done that way though.

 

<?php 

$spec = array(
'Y' => array('A','B'),
'M' => array('A','B','C')
);

$str = 'ABYABM';

$combos = getCombos( $str, $spec );

print_r( $combos );

function getCombos( $input, $special ) {

// $r will hold our final array to return
// str_split turns our string of letters into an array
$r = array(); $letters = str_split( $input );
// loop through each letter
foreach( $letters as $letter ) {
	// check to see if it's a special letter
	if( array_key_exists($letter,$special) ) {
		// if the first letter is special, we can just start $r with the
		// letters the special letter represents
		if( empty($r) ) $r = $special[$letter];
		// otherwise we'll have to rebuild $r
		else {
			// start off with a clean array
			$newR = array();
			// loop through each letter the special letter represents
			foreach( $special[$letter] as $specLetter )
				// then loop through each existing combination, appending the
				// current representative letter.
				foreach( $r as $return ) $newR[] = $return . $specLetter;
			// We then replace $r with our newly rebuilt version of it.
			$r = $newR;
		}
	// otherwise, it's not a special letter	
	} else {
		// if it's the first letter, we can start $r with it
		if( empty($r) ) $r[0] = $letter;
		// Otherwise, we have to loop through each existing combination and
		// append this letter to it
		else foreach( $r as $key => $return ) $r[$key] .= $letter;
	}

}

// Everything should be built. $r should have as many keys as the product of all
// amounts of special characters used. If you gave it a string like 'AYBYCMAB' you
// should have count(y) * count(y) * count(m) or 2*2*3 or 12 keys
return $r;

}

?>

 

Outputs

 

Array
(
    [0] => ABAABA
    [1] => ABBABA
    [2] => ABAABB
    [3] => ABBABB
    [4] => ABAABC
    [5] => ABBABC
)

 

Comments done

Link to comment
Share on other sites

Loops inside loops can get ugly. I think it over step by step, and use a lot of echo's to help visualize what's happening to my strings, and what I want done to them.

 

If you're curious, I can show you the recursive version, for educational purposes. That's assuming you understand my code and what it does.

Link to comment
Share on other sites

Yeah i understand your code, just trying to adapt it in to what i'm building, as the variables come in, in an array, using a forloop to seperate each one and then run your code to change the wildcards.

 

Is there anyway to add the combinations back on to the orignal array?

 

$string = $_POST["string"];
if ($string){ 
foreach ($string as $str){
$spec = array(
'Y' => array('A','B'),
'M' => array('A','B','C')
);

$combos = getCombos( $str, $spec);

print_r( $combos );

}
}

Link to comment
Share on other sites

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.