PHP - Combinations & Filtering Permutations
I'm trying to firstly generate all possible 'x' number combinations from an array of 'y' numbers. i.e. combinations of 3 numbers from array(1,2,3,4,5). so... 1,2,3 - 1,2,4 - 1,2,5, 1,3,4 etc
I then want to be able to filter out duplicate permutations. i.e. 1,2,3 is the same as 1,3,2. I have googled vigorously but not been able to determine a suitable approach. Any suggestions would be appreciated. Thanks. Similar TutorialsHello, Am looking for a function that generates all possible combinations of strings in an array. For example if: $arr[1] = "Hello"; $arr[1] = "how"; $arr[2] = "are"; $arr[3] = "you"; When the array is passed through the function you would come up with a list of string combinations line by line like below: Hello Hello how are you how Hello are you how are Hello you how are you Hello how ........and so on The list goes on and on. Ok, So what I need is a way for arrays to combine. My basic structure is going to look like this for the form that needs submission: Code: [Select] <form> <input name="name[]" /> <input name="name1[]" /> <input name="name[]" /> <input name="name1[]" /> <input name="name[]" /> <input name="name1[]" /> </form>then on the page it submits to, I need an array to form that will combine all the inputs to looks something like this: "name" "name1"; "name" "name1"; "name" "name1";.......... where the first set is the first name and name1, second is second, third is third, etc. The string of arrays will only end when there are no more left, so if there were 6, it would combine 6. how do I do something like this? Hi. I need some help working something out. I have an array that has all the numbers from, 0 to 9. What I want to do is loop through the array to make all the combinations possible up to a string thats 255 chars long. $array = array( '0', '1', '2', '3', '4', '5', '6', '7', '8', '9' ); Should be looped through to create strings like this: Code: [Select] 0 1 2 3 4 5 6 7 8 9 00 01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 19 20 21 22 23 etc.. Just as if it's counting. Hi PHPfreaks, I've got a problem Ive been faced with a few days now, and for the life of me Im unable to solve it. What I have is a multidimensional array, that I need to parse and retreive every possible outcome possible Below is a basic example of how the array would look Code: [Select] Colour Black White Blue Finish Matt Gloss What I need to be able to do is, as I said, Show all the possible combinations there is. below is what i'd have achieved after one of you helpful souls points me in the right direction. Code: [Select] --> Color: Black --> Color: White --> Color: Blue --> Finish: Matt --> Finish: Matt --> Color: Black, Finish: Matt --> Color: White, Finish: Matt --> Color: Blue, Finish: Matt --> Color: Black, Finish: Gloss --> Color: White, Finish: Gloss --> Color: Blue, Finish: Gloss As you can see, Only ONE (or no) value is picked from each section. Just to confirm: the number of option groups WILL change...... So I could have colour, finish, size, weight and many more. the number of values in the groups WILL change....... so under colour, I could also have pink, green, indigo, gray. Below is how this array is structured Code: [Select] Array ( [0] => Array ( [parent] => Colour [values] => Array ( [0] => Array ( [name] => Black ) [1] => Array ( [name] => White ) ) ) [1] => Array ( [parent] => Finish [values] => Array ( [0] => Array ( [name] => Matt ) [1] => Array ( [name] => Gloss ) ) ) ) Would anyone have any suggestion on how this can be done? I've pulled out every single strand of hair I've got now, so thought i'd admit defeat and let someone else have a go Many thanks inadvance. I searched online and seen some complex c and c# code which I didn't understand much of. But how do I generate an array with all string combinations based on length and from a charset? E.g. charset = {'a', 'b', 'c'} Length = 2. Then it should generate first all 1-char strings: a b c Then all 2-char strings: aa ab ac ba bb bc ca cb cc And then all added to the same array. I think there's some way of doing it with recursion but to me it's like thinking with a 4th dimension my mind can't grasp this logic! need to echo all word combinations from the input also lets say that someone types "this is my query" into an input fiend and this will be the input. i need the php script to echo all the possible combinations of this string. but there can be 4 words like in the example or 3 or more or less. also it should then echo something like this: this this is this is my this is my query is is my is my query is my query this my my query my query this my query this is query query this query this is query this is my + query is my this this my query is also all combinations possible, for 1 word, 2 words, ... max. words inputed. I have a form and script that is used to collect information from users. There is one field that is url. There is one field that is email. I want to filter out scripts. I also only want to allow url in the url field and email in the email field. Is there an easy way to filter? I have tried several pieces of code to no avail. Code: [Select] <?php $root = $_SERVER['DOCUMENT_ROOT']; include('/home/arts/public_html/shows/includes/config.db.php'); if ($link) { foreach ($_POST as $k => $v) { if (($k == "start_date") || ($k == "end_date") || ($k == "application_dead")) { $varray = explode("/",$v); $v = $varray[2] . "-" . $varray[0] . "-" . $varray[1]; } $v = "'" . addslashes($v) . "'"; if($k != "captcha_code") { $keys[] = $k; $vals[] = $v; } } $fields = implode(",", $keys); $values = implode(",", $vals); //$fields = "id," . $fields; //$values = "null," . $values; $sql = "INSERT into craft_shows ($fields) VALUES ($values)"; $result = mysql_query($sql,$link); if (!$result) { die('There was a problem with your submission. Refresh the page to try again'); } else { echo "Thank you for your submission.<br>"; echo "<a href=\"http://www.artsandcraftsnetwork.com/shows/\">Back to shows</a><br>"; echo "<a href=\"http://www.artsandcraftsnetwork.com/shows/shows_submit.php\">Submit another show</a>"; } } ?> I have a SQL statement which is difficult to use PDO on, it might not even be possible to do.
So I'm filtering it like this:
$search = $_GET['search']; $search = preg_replace("/[^A-Za-z0-9]/", " ", $search); $search = $mysqli->real_escape_string($search);Will this result in an acceptable level of security? Edited by anderson_catchme, 16 September 2014 - 12:28 PM. I want to create a feature as you see nn this image, where it says 'filter your results' if a user clicks 'Detached Houses' then only detached houses will be displayed. if a user clicks 'Semi-detached' then only semi detached houses will be shown. Any help is really appreciated, thank you. Code: [Select] <table border="0" cellpadding="0" cellspacing="0"> <tr> <td><div id="filter"><p class="houses" style="font-family:helvetica;color:#0155a1;font-size:14px;background:url(cutouts/forsale/filter.jpg) no-repeat;"><b><u>Houses</u></b> <br /> <span class="dh"><b><u>Detached Houses</u></b></span><?php // Make a MySQL Connection mysql_connect("localhost", "admin", "1admin") or die(mysql_error()); mysql_select_db("test") or die(mysql_error()); // Get a specific result from the "example" table $result = mysql_query("SELECT * FROM example WHERE name='Sandy Smith'") or die(mysql_error()); // get the first (and hopefully only) entry from the result $row = mysql_fetch_array( $result ); // Print out the contents of each row into a table echo $row['name']." - ".$row['age']; ?> <br /> <span class="dh"><b><u>Semi-detached houses</u></b></span> <br /> <span class="dh"><b><u>Terraced houses</u></b></span> <br /> <br /> <b><u>Flats / Apartments</u></b> </p></div></td> Hi guys I was wondering if anyone could point me in the right direction with this. I am using JSON to return the contents of a directory and then using a foreach loop to print each one to a new row in a table. Is there anyway to filter the results? Basically I just want to show the files that end in a .jar extension? Currently all sub directories and files are shown, however all the files I need to pull will be in the main directory. This is what I am using: Code: [Select] <?php foreach ($installedplugins1['success'] as $v) { echo "<tr><td>".$v."</td>"; echo "<td><a href='index.php?dp=".$v."'>Disable Plugin</a></td>"; } ?> Any hints or reading material? Cheers Hi there I have the following array . Code: [Select] $products = array( array( Code => "CF1", Items => "Sand Tray", Cat => 'Tray', ), array( Code => "CF2", Items => "Mobile Computer Table", Cat => 'Table', ), array( Code => "CF3", Items => "General Service Trolley", Cat => 'Trolley', ), array( Code => "CF4", Items => "TV Trolley", Cat => 'Trolley', ), array( Code => "CF5", Items => "Overhead Projector Trolley", Cat => 'Trolley', ), array( Code => "CF6", Items => "Book Trolley", Cat => 'Tolley', ), array( Code => "CF7", Items => "Stacking Chairs", Cat => 'Chairs', ), ); What I want to do is select each by 'Cat'. For example I simply want to display all the trolleys. I was wondering how I do this? What functions would I use in PHP? Thanks a million hey guys i was generally wondering...is it good practice to add_slashes and use mysqli_real_escape_string when entering data into the database?
then to strip slashes when extracting rows?
is this the right way to go around things...thanks
This topic has been moved to PHP Regex. http://www.phpfreaks.com/forums/index.php?topic=334433.0 Hi all, I am working on a tool to pull data remotely and the data is set to xml format. I want to post items that meet a certain criteria only, essentially there are several layers/tags on the data being brought over. The sStatus is the primary item I am looking for, not only that but only if it has or equals to "Waiting". Any thoughts or suggestions greatly appreciated. Below is the basic code I have in place currently and am working on. <code> <?php $soapclient = new SoapClient("https://secure.logmeinrescue.com/api/api.asmx?wsdl"); $sEmail = "dummy@dum.com"; $sPassword = "dum123"; $iNodeID = "123456"; $eNodeRef = "NODE"; $sAuthCode = ""; $loginparams = array ( 'sEmail' => $sEmail, 'sPassword' => $sPassword); $loginResult = $soapclient->login($loginparams); $output = array( 'eOutput' => "XML" ); $outputResponse = $soapclient->setOutput($output); $sessionparams = array( 'iNodeID' => $iNodeID, 'eNodeRef' => $eNodeRef, 'sAuthCode' => $sAuthCode ); //get session(s) $sessionresult = $soapclient->getSession($sessionparams); $session = $sessionresult->aSessions; $sessionnodes = $session->SESSION; $session_status = $sessionnodes[$isessionNodes]->sStatus; print_r($sessionresult); ?> </code> hi, i am trying to filter my gps history points. i would like to ignore points if they are too close to the previous position. i can do this like so. $old_lat = $old_long = "0"; foreach($history['data'] as $record) { //calculate distance in meters $distance = distance($record['latitude'], $record['longitude'], $old_lat, $old_long, "V"); if($distance >= 300) { echo 'add(jQuery(this), number += 1, "' . date("d-m-Y @ h:i:s",$record['timestamp']) . '", "map_post.php?n=' . $name . 'u=' . $history['user'] . '", "' . $history['user'] . '", "' . $record['latitude'] . '", "' . $record['longitude'] . '");'; $old_lat = $record['latitude']; $old_long = $record['longitude']; } the problem with the above is that if i arrive home at 6pm on friday and stay outside my house for 24hours, it will show the latest position as at 6pm on fri. i would like it to show the 6pm friday position but with a date 'from and to' time like (28/10/11 18:00 - 29/10/11 18:00) rarther than just (28/10/11 18:00) which looks inacurate. Possible? Hello,
Is it possible to allow '@' and '.' while maintaining the rest of this regex?
$email= 'adsfsa@asdf.edu'; $email= preg_replace("/[^A-Za-z0-9]/", " ", $email); echo $email; I'm pretty sure the problem is starring me in the face, but I can't seem to locate it. function filter($string) { //swear words pulled from bannedwordlist.com $f = fopen('../badwords.txt', 'r'); $bad_words = fread($f, filesize('../badwords.txt')); $bad_words = explode('\n', $bad_words); $input = strtolower($string); foreach($bad_words as $value) { $string = str_replace($value, '****', $input); } return $string; } UPDATED CODE function filter($string) { //swear words pulled from bannedwordlist.com $f = fopen('../badwords.txt', 'r'); $bad_words = fread($f, filesize('../badwords.txt')); $bad_words = explode('\n', $bad_words); $input = strtolower($string); $string = str_replace($bad_words, '****', $input); return $string; } I've already echoed out $value in the foreeach loop, and it does correctly retrieve the bad words and put them into an array. My only problem is, the returned string is still in strtolower() form, and the words aren't censored. :/ Hi, I have an issue, I cant filter my data from DB. Whats wrong in my code? <?php /* Include Files *********************/ //include("banners/database.php"); mysql_connect("host", "user", "pwd") or die("Connection Failed"); mysql_select_db("DB")or die("Connection Failed"); ?> <?php $memberIndustries =['memberIndustries']; if (isset($_POST["memberIndustries"])) { $memberIndustries = mysql_real_escape_string ($_POST["memberIndustries"]); $sql="SELECT * FROM Members WHERE Industries_Industry_ID='$memberIndustries'"; $result = mysql_query($sql) or die (mysql_error()); while ($myrowe = mysql_fetch_array($result)) {?> <? // if new member, label it //$todays_date = date("Y-m-d"); //$dateCompare = $myrowe[Creation_Date]+30; //$dateCompare = strtotime(date("Y-m-d", strtotime($myrowe[Creation_Date])) . " +30 days"); //if(strtotime($todays_date)<=$dateCompare){ ?> <table width="100%"> <tr> <td align="right"> <img src="/members/new.gif"> </td> </tr> </table> <? //}?> <? if($myrowe[LOGO_lnk]!='') {?> <img src="<? echo $myrowe[LOGO_lnk]; ?>"> <? } ?> <br /> <br /> <font style="font-family: Arial; font-size: 10.5pt; font-weight:bold;color:#C1121F"> <? echo $myrowe[Company_Title];?> </font> <? if($myrowe[Person_Name_1]!='') {?> <br /> <font style="font-family: Arial; font-size: 8.5pt; font-weight:bold;color:#000000"><? echo $myrowe[Person_Name_1];?></font> <font style="font-family: Arial; font-size: 8.5pt; font-weight:bold;color:#000000"><? echo $myrowe[Person_Title_1];?></font> <? } if($myrowe[Person_Name_2]!='') {?> <br /> <font style="font-family: Arial; font-size: 8.5pt; font-weight:bold;color:#000000"><? echo $myrowe[Person_Name_2];?></font> <font style="font-family: Arial; font-size: 8.5pt; font-weight:bold;color:#000000"><? echo $myrowe[Person_Title_2];?></font> <? } if($myrowe[Person_Name_2]!='') {?> <br /> <font style="font-family: Arial; font-size: 8.5pt; font-weight:bold;color:#000000"><? echo $myrowe[Person_Name_3];?></font> <font style="font-family: Arial; font-size: 8.5pt; font-weight:bold;color:#000000"><? echo $myrowe[Person_Title_3];?></font> <? } if($myrowe[Address_row1]!='') {?> <br /> <font style="font-family: Arial; font-size: 8.5pt; color:#000000"><? echo $myrowe[Address_row1];?></font> <? } if($myrowe[Address_row_2]!='') {?> <br /> <font style="font-family: Arial; font-size: 8.5pt; color:#000000"><? echo $myrowe[Address_row_2];?></font> <? } if($myrowe[Address_row_3]!='') {?> <br /> <font style="font-family: Arial; font-size: 8.5pt; color:#000000"><? echo $myrowe[Address_row_3];?></font> <? } if($myrowe[Phone]!='') {?> <br /> <font style="font-family: Arial; font-size: 8.5pt; color:#000000">Tel:<? echo $myrowe[Phone];?></font> <? } if($myrowe[Fax]!='') {?> <br /> <font style="font-family: Arial; font-size: 8.5pt; color:#000000">Fax:<? echo $myrowe[Fax];?></font> <? } if($myrowe[email]!='') {?> <br /> <a href="mailto:<? echo $myrowe[email];?>"> <font style="font-family: Arial; font-size: 8.5pt; color:#000000"><? echo $myrowe[email];?></font> </a> <? } if($myrowe[web]!='') {?> <br /> <a href="<? echo $myrowe[web];?>"target="_blank"> <font style="font-family: Arial; font-size: 8.5pt; color:#000000"><? echo $myrowe[web];?></font> </a> <? } if($myrowe[Industries_Industry_ID]!=0) { $queryInd = "SELECT Industry_Title FROM Industries WHERE Industry_ID=$myrowe[Industries_Industry_ID]"; $resultInd = mysql_query($queryInd) or die (mysql_error()); $myroweIND = mysql_fetch_array($resultInd) ?> <br /> <font style="font-family: Arial; font-size: 8.5pt; color:#000000"><? echo $myroweIND[Industry_Title];?></font> <? } if($myrowe[Description]!='') {?> <br /> <font style="font-family: Arial; font-size: 8.5pt; color:#000000"><? echo $myrowe[Description];?></font><p></p> <? } ?> <br /><br /> <? } <? }} ?> Hi all, I was wondering if i can filter certain log files like using 2 keywords and get all the info inbetween? The log files are like <1 MB. The other problem i encounter is howto since i looked all over the net but cant seem to find usefull info about it. Can you help me out here? |