473,382 Members | 1,204 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

How do you get PHP to print this...

I am starting with a string. Let's say that string is "horse.gallup." 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
Jun 24 '08 #1
4 1348
hsriat
1,654 Expert 1GB
See if this suits your needs...
[PHP]<?php

$input = "horse.gallup";

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

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

foreach($new_prefix as $key=>$value)
$new_suffix[$key] = $value.".".$suffix;

echo "<pre>";
print_r($new_suffix);

unset($new_suffix);
}

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

$flags_array = get_combination_flags(count($word_array), $length);
foreach ($flags_array as $flags)
{

foreach ($flags as $key=>$flag)
if ($flag == 0)
unset($word_array[$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(decbin($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($flag_array, str_split($temp));
}
}
return $flag_array;
}
?>[/PHP]
Jun 24 '08 #2
Another code for your weird problem:

Expand|Select|Wrap|Line Numbers
  1. <?php
  2. $input = 'horse.gallup';
  3.  
  4. preg_match("/^([\w]+).([\w]+)$/", $input, $matches);
  5.  
  6. $prefix = $matches[1];
  7. $suffix = $matches[2];
  8.  
  9. for ($i = 0 ; $i < strlen($prefix) ; $i++) {
  10.     $a = substr($prefix, 0, $i);
  11.     $b = substr($prefix, $i+1, strlen($prefix));
  12.  
  13.     $text = "$a$b.$suffix";
  14.     print "$text<br>";
  15. }
  16. ?>
  17.  
Jun 24 '08 #3
Thank you so much. That worked great.
Jun 24 '08 #4
dlite922
1,584 Expert 1GB
Thank you so much. That worked great.
who's solution worked or are you referring to?
Jun 24 '08 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

12
by: Michael Foord | last post by:
Here's a little oddity with 'print' being a reserved word... >>> class thing: pass >>> something = thing() >>> something.print = 3 SyntaxError: invalid syntax >>> print something.__dict__...
14
by: Marcin Ciura | last post by:
Here is a pre-PEP about print that I wrote recently. Please let me know what is the community's opinion on it. Cheers, Marcin PEP: XXX Title: Print Without Intervening Space Version:...
4
by: Fred | last post by:
I'm a newbie to JavaScript so be gentle. You know that funky IE margin issue that causes the right side of the page to get cut off when the user prints the html document?...
6
by: Dennis Allen | last post by:
Hi. On one particular web page I want to offer the user a print button. Question. How do I create one? Something compatible with most browsers would be nice. Appreciate any advice...Dennis
2
by: Dean Slindee | last post by:
Anybody written code in VB.NET to: 1) show a print preview window of reports already written and stored in an Access 2002 database; or 2) execute the print of a report stored in an Access 2002...
1
by: Steff | last post by:
I am wandering if my code is making sense... I use a lot the print function. Is it weird in this case where I have to display an array ? I thought it would be better to have the entire array in php...
2
by: kbperry | last post by:
Hi all, I am getting an error message when trying to use the P4 print command via the python api for perforce. Anytime that I run p4c.run("print","-q", eachFile), I keep getting an error...
69
by: Edward K Ream | last post by:
The pros and cons of making 'print' a function in Python 3.x are well discussed at: http://mail.python.org/pipermail/python-dev/2005-September/056154.html Alas, it appears that the effect of...
2
by: Brad Pears | last post by:
I have some sample code that uses the print dialog, print preview and a print direct options. If I select print preview and then click the printer icon from that, the document prints. If I...
11
by: JWest46088 | last post by:
I'm having difficulty trying to figure out how to print a text file from a hash table one line at a time. I have the text file read into the hash table and can print the text file all at once, but I...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.