How do you get PHP to print this...

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • lazukars
    New Member
    • Mar 2007
    • 11

    How do you get PHP to print this...

    I am starting with a string. Let's say that string is "horse.gall up." Now I need to print out every variation of horse without one if its letters plus the ending ".gallup". So the result should be as follows.

    orse.gallup
    hrse.gallup
    hose.gallup
    hore.gallup
    hors.gallup

    Now if someone could figure out how to get the above results with any string that contains a period within it, that would be awesome.

    Thanks in advance for any help. I is really appreciated.

    Sincerely,
    Ryan
  • hsriat
    Recognized Expert Top Contributor
    • Jan 2008
    • 1653

    #2
    See if this suits your needs...
    [PHP]<?php

    $input = "horse.gall up";

    $prefix = strtok($input, ".");
    $suffix = strtok(".");

    for ($i = strlen($prefix) ; $i>0; $i--)
    {
    $new_prefix = make_new_combin ations($prefix, $i);

    foreach($new_pr efix as $key=>$value)
    $new_suffix[$key] = $value.".".$suf fix;

    echo "<pre>";
    print_r($new_su ffix);

    unset($new_suff ix);
    }

    function make_new_combin ations($word, $length)
    {
    $word_array = str_split($word );
    $new_words = array();

    $flags_array = get_combination _flags(count($w ord_array), $length);
    foreach ($flags_array as $flags)
    {

    foreach ($flags as $key=>$flag)
    if ($flag == 0)
    unset($word_arr ay[$key]);

    array_push($new _words, implode("", $word_array));

    $word_array = str_split($word );
    }
    return $new_words;
    }


    function get_combination _flags($total, $required)
    {
    $flag_array = array();
    $bin = "";

    for ($i=0; $i<$total; $i++)
    $bin .= "1";
    $dec = bindec($bin);

    for ($i=0; $i<=$dec; $i++)
    {
    $num_of_chars = count_chars(dec bin($i), 1);
    if ($num_of_chars[49]==$required)
    {
    $temp = decbin($i);
    $length = strlen($temp);
    $pre = "";

    if ($length<$total )
    for ($j=$length; $j<$total; $j++)
    $pre .= "0";

    $temp = $pre.$temp;
    array_push($fla g_array, str_split($temp ));
    }
    }
    return $flag_array;
    }
    ?>[/PHP]

    Comment

    • nashruddin
      New Member
      • Jun 2008
      • 25

      #3
      Another code for your weird problem:

      Code:
      <?php
      $input = 'horse.gallup';
      
      preg_match("/^([\w]+).([\w]+)$/", $input, $matches);
      
      $prefix = $matches[1];
      $suffix = $matches[2];
      
      for ($i = 0 ; $i < strlen($prefix) ; $i++) {
      	$a = substr($prefix, 0, $i);
      	$b = substr($prefix, $i+1, strlen($prefix));
      
      	$text = "$a$b.$suffix";
      	print "$text<br>";
      }
      ?>

      Comment

      • lazukars
        New Member
        • Mar 2007
        • 11

        #4
        Thank you so much. That worked great.

        Comment

        • dlite922
          Recognized Expert Top Contributor
          • Dec 2007
          • 1586

          #5
          Originally posted by lazukars
          Thank you so much. That worked great.
          who's solution worked or are you referring to?

          Comment

          Working...