Drompo2 Posted July 6, 2011 Share Posted July 6, 2011 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 Quote Link to comment Share on other sites More sharing options...
xyph Posted July 6, 2011 Share Posted July 6, 2011 You have to be more specific. Give examples? Quote Link to comment Share on other sites More sharing options...
Drompo2 Posted July 6, 2011 Author Share Posted July 6, 2011 If a user entered string is ABYABM and Y can mean A or B and M can be A, B or C The string could be ABAABA ABBABA ABAABB ABBABB ABAABC ABBABC Quote Link to comment Share on other sites More sharing options...
xyph Posted July 6, 2011 Share Posted July 6, 2011 This could get in to quite a few loops. Code coming up in a sec Quote Link to comment Share on other sites More sharing options...
Drompo2 Posted July 6, 2011 Author Share Posted July 6, 2011 Yeah i assumed it would contain many loops, check for each instance. Thank you for your time Quote Link to comment Share on other sites More sharing options...
xyph Posted July 6, 2011 Share Posted July 6, 2011 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 Quote Link to comment Share on other sites More sharing options...
Drompo2 Posted July 6, 2011 Author Share Posted July 6, 2011 Works Perfectly, Thank you ever so much! Been doing PHP for years and i couldnt get my head around this at all. Quote Link to comment Share on other sites More sharing options...
xyph Posted July 6, 2011 Share Posted July 6, 2011 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. Quote Link to comment Share on other sites More sharing options...
Drompo2 Posted July 6, 2011 Author Share Posted July 6, 2011 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 ); } } Quote Link to comment Share on other sites More sharing options...
Drompo2 Posted July 6, 2011 Author Share Posted July 6, 2011 Managed to solve previous post. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.