Jump to content

It's time to Preg_MATCH!


Monkuar

Recommended Posts

This is just getting ridiculous, I blame it on myself really.

 

 

 

These are my Balls seperated by "," each seperated , is a "Ticket" with 3 chosen balls (A Lottery system Im making) 

 

User needs to match them "IN ORDER" to win jackpot.

 

$numbers_chosen = "8|18|3,18|3|8,|3|8|18";
$winning_numbers = "8|18|3";

 

 

How would I go about in a preg_match statement  through each array (Ticket) to see if it matches "Specifically" with 8|18|3, and if there is a Match (IN ORDER) I echo out true?

 

Here is what I am working with.

 

if (preg_match("/8|18|3/i", "8|18|3,18|3|8,|3|8|18")) {
//True
    echo "Code here to give that player the winning Lottery";
} else {
//False
    echo "User has no Winning Tickets, failed.";
}

 

For some reason whenever I change it around, it does not seem to work any ideas?

 

the /i is doing something Inappropriate to the code perhaps?

 

 

Link to comment
Share on other sites

There should be no need to use a regex pattern for this.

 

if( strpos($numbers_chosen, $winning_numbers) !== FALSE ) {
echo "We have a winner";
} else {
echo "Better luck next time, loser.";
}

 

$numbers_chosen = "8|18|32,18|3|8,|3|8|18";
$winning_numbers = "8|18|3";
if( strpos($numbers_chosen, $winning_numbers) !== FALSE ) {
 echo "Code here to give that player the winning Lottery <br><br><br>";
} else {
echo "User has no Winning Tickets, failed.";
}

 

Returns true? I changed the numbers chosen 8|18|3 to 8|18|32, should return false?

Link to comment
Share on other sites

Ah, yes. You're right. I didn't take that into consideration. I just shifted numbers around when I tested it.

 

NP

 

I have this to

 

$mystring = '8|18|32,18|3|8,|3|8|18';
$findme   = '8|18|3';
$pos = strpos($mystring, $findme);
// Note our use of ===.  Simply == would not work as expected
// because the position of 'a' was the 0th (first) character.
if ($pos === false) {
    echo "The string '$findme' was not found in the string '$mystring'";
} else {
    echo "The string '$findme' was found in the string '$mystring'";
    echo " and exists at position $pos";
}

 

 

Still returns true! haha no idea man, how the hell is it returning true when  8|18|32 DOES NOT EQUAL 8|18|3 ??

 

/phpowned

 

somehow i need to get this damn thing into a matchable array?

 

Link to comment
Share on other sites

This should work as expected.

 

$numbers_chosen = "8|18|3,3|8|18,|3|18|8";
$winning_numbers = "3|8|18";
$chosen = explode(',', $numbers_chosen);
foreach($chosen as $v ) {
$winner = FALSE;
if(strcmp($winning_numbers, $v) === 0 ) {
	$winner = TRUE;
	break;
}
}
if( $winner ) {
echo "We have a winner";
} else {
echo "No soup for you!";
}

Link to comment
Share on other sites

You can compare arrays with comparison operators which would take the order into account:

 

<?php
$numbers_chosen = "8|18|3,18|3|8,3|8|18";
$winning_numbers = "8|18|3";

// Let's get those winning numbers into an array
$winning_numbers = explode('|', $winning_numbers);

$winner = false;
$tickets = explode(',', $numbers_chosen);
foreach ($tickets as $ticket) {
$balls = explode('|', $ticket);
if ($balls == $winning_numbers) $winner = true;  # This is where we compare the arrays (including order)
}

echo ($winner) ? "We have a winner" : "Try again.";
exit;
?>

Link to comment
Share on other sites

Ah, yes. You're right. I didn't take that into consideration. I just shifted numbers around when I tested it.

 

NP

 

I have this to

 

$mystring = '8|18|32,18|3|8,|3|8|18';
$findme   = '8|18|3';
$pos = strpos($mystring, $findme);
// Note our use of ===.  Simply == would not work as expected
// because the position of 'a' was the 0th (first) character.
if ($pos === false) {
    echo "The string '$findme' was not found in the string '$mystring'";
} else {
    echo "The string '$findme' was found in the string '$mystring'";
    echo " and exists at position $pos";
}

 

 

Still returns true! haha no idea man, how the hell is it returning true when  8|18|32 DOES NOT EQUAL 8|18|3 ??

 

/phpowned

 

somehow i need to get this damn thing into a matchable array?

 

 

The example you posted above returns true because 8|18|3 actually does exist within the other string, here: 8|18|32,18|3|8,|3|8|18.

Link to comment
Share on other sites

This should work as expected.

 

$numbers_chosen = "8|18|3,3|8|18,|3|18|8";
$winning_numbers = "3|8|18";
$chosen = explode(',', $numbers_chosen);
foreach($chosen as $v ) {
$winner = FALSE;
if(strcmp($winning_numbers, $v) === 0 ) {
	$winner = TRUE;
	break;
}
}
if( $winner ) {
echo "We have a winner";
} else {
echo "No soup for you!";
}

 

Hey, was wondering, how did you pass the "|" (Pipes) through the for each? I see no explode for them, (As it's not needed?) can you explain a little bit more how the code works, I really want to learn from it now.  Because you help me so much ^^

 

I guess it makes each , a array and checks each individual 1 for the matching winning_number variable? if true = winner = TRUE right? But how in the world is there no functions/code to help with the "|'s? or PIPES, wouldn't you have to use something to make those not get into the way? thanks

 

 

behicthebuilder CODE explains it good too, Thank you

 

 

See now Im confused on ben's code he exploded the |'s, why wasn't this needed on Pikachus?

Link to comment
Share on other sites

Since the numbers are listed with the pipes, and the order has to be the same, if you explode the $chosen string, you get an array with each set of chosen numbers in its own element, which you can compare directly with the string of winning numbers. This assumes your format remains as it is in the code you posted, of course.

Link to comment
Share on other sites

Since the numbers are listed with the pipes, and the order has to be the same, if you explode the $chosen string, you get an array with each set of chosen numbers in its own element, which you can compare directly with the string of winning numbers. This assumes your format remains as it is in the code you posted, of course.

 

Yeah it will, forever.  ::)

 

It's just weird ben's exploded the |'s but both codes seem to work. Not to be rude, but do you think yours is a little faster then bens? Cause you don't need to explode the |'s so I assume would be better? Or the performance is so minimal it doesn't matter?

 

 

 

Okay, I will be messing with the codes now to try to get a "Loose Match" if the user has matched "2" numbers per Ticket, the same way how this was made, but (Finds the first 2 matching in order or NOT) I am going to try to figure it out on my own then post here for help, I don't want to keep cheating myself, I will learn nothing.  Then if I get stuck on it for couple hours i will post here.  (User will ofcourse win less Points this way) Easier to win

 

Thnak you PFMaBiSmAd, makes little more since now.

 

This way is alot simpler then multidimensional arrays,

Link to comment
Share on other sites

It's just weird ben's exploded the |'s but both codes seem to work. Not to be rude, but do you think yours is a little faster then bens? Cause you don't need to explode the |'s so I assume would be better? Or the performance is so minimal it doesn't matter?

 

There are many ways to do this (and probably more) as you've just discovered:

 

1. using in_array() after exploding by comma

2. strcmp after exploding by comma

3. array comparison after exploding by comma and then by pipe

 

I suspect that both the first two are faster than the third.  But I do recall reading another thread of yours where you suggested calculating how close the user's balls were to jackpot - in which case having each chosen ball as an array value would be useful.

 

Also, unless this is for a ridiculously large lottery, I doubt you would notice the performance difference.

Link to comment
Share on other sites

92604493.jpg

 

 

//Check Exact winnings/Jackpot
$winning_numbers = "3|8|18";
$numbers_chosen = "3|8|19,3|22|18,|2|18|8,|2|18|8,|2|18|8,|2|18|8";
$chosen = explode(',', $numbers_chosen);
foreach($chosen as $v ) {
$winner = FALSE;
if(strcmp($winning_numbers, $v) === 0 ) {
//User has matched all 3 Numbers! GIVE THEM JACKPOT = TRUE!
$jackpot = TRUE;
}else{
//User has NOT Matched any, check if they matched any 2 In a row
echo "?";
}
}
if( $jackpot ) {
echo "We have a winner";
} else {
echo "No soup for you!";
}

 

Okay, now im on to if the match failed, now check for "Loose" Winnings, match the FIRST "2" in a order, set another variable to true! :)

 

Im stuck again  :confused:  Somehow I need to need to check if they are only matching 2 times (Should it be any number in the Ticket) or the first 2? No idea Either way would work I imagine (but only would match the first 2) then stop so I would be using strcmp again I assume

 

 

o.o I think im getting closer

 

}else if(strcmp($winning_numbers, $v) === 1 ) {
//User has NOT Matched any, check if they matched any 2 In a row
echo "I matched 1 <br>";

Link to comment
Share on other sites

You need to decide on whether you are telling the user they got 2/3 numbers correct or 2/3 numbers correct AND in the correct position.  This makes quite a difference in terms of difficulty.

 

Then keep in mind that the first and third balls might be correct, or the first and second, or the second and third so that's a lot of possibilities to account for.  This is where using arrays instead of string comparisons might be easier.  I can easily think of how to do either scenario with arrays but maybe one of the other forum prowlers can suggest some magic with strings if you're going down that road.

 

But first decide how much of a match you want the check for 2/3 correct balls to be (just numbers or numbers and position)?.

Link to comment
Share on other sites

You need to decide on whether you are telling the user they got 2/3 numbers correct or 2/3 numbers correct AND in the correct position.  This makes quite a difference in terms of difficulty.

 

Then keep in mind that the first and third balls might be correct, or the first and second, or the second and third so that's a lot of possibilities to account for.  This is where using arrays instead of string comparisons might be easier.  I can easily think of how to do either scenario with arrays but maybe one of the other forum prowlers can suggest some magic with strings if you're going down that road.

 

But first decide how much of a match you want the check for 2/3 correct balls to be (just numbers or numbers and position)?.

 

Well,

Let's me see:

 

Match all 3 numbers of next drawing in the same exact order drawn to win Jackpot. (First match found wins) 100% Jackpot

Match all 3 numbers, out of order, and win 30% of the JackPot

Match the first 2 numbers in order, and win 20% of the JackPot

Match any 2 numbers drawn, and win 5% of the Jackpot. (get's the first match?)

 

(all Come first, whoever purchased there ticket first, so If I have 2000 users all that have purchased balls, it will run the for loop and once a jackpot found, exit the script and reset the lottery (Which I have code for that)

 

I am really bad at explaining, but a simpliar way to explain is

 

The exact function works, we have that code now you guys made for me, I love it.  So now my System works, it's awesome. but I think I just need to add a easier way to win the jackpot you know? Matching all 3 balls out of 36 numbers is a 1 in 1:42840 chance, that is not fair for my users, so a "loose" way, like if they matched the first 2 numbers in ORDER, win 20% of the pot. and so on, I can also modify the code too if I get a head start from you guys.

 

:)

Link to comment
Share on other sites

Ok, I took an array approach here.  I'll leave working out when all the winnings have been depleted up to you.  For example, you can't have 5 users win the second tier and 3 users win the third tier - because that would total 210% of the winnings (i.e. you're bankrupt).

 

<?php
$numbers_chosen = "8|18|3,18|8|3,8|18|20,8|30|3";
$winning_numbers = "8|18|3";

// Let's get those winning numbers into an array
$jackpot = explode('|', $winning_numbers);

$tiers_won = array();
$tickets = explode(',', $numbers_chosen);
foreach ($tickets as $ticket) {
$balls = explode('|', $ticket);
if ($balls == $jackpot) {
	// All three balls match the jackpot (in the right order too)
	$tiers_won[] = 100;		
}
elseif ( ! array_diff($balls, $jackpot)) {
	// All the balls are the same but not necessarily in the correct order.
	$tiers_won[] = 30;		
}
elseif ($balls[0] == $jackpot[0] AND $balls[1] == $jackpot[1]) {
	// The first two balls match those of the jackpot
	$tiers_won[] = 20;		
}
elseif (count(array_diff($balls, $jackpot)) == 1) {
	// Only one ball is a mismatch (not considering the order)
	$tiers_won[] = 5;		
}
}

if ($tiers_won) {
foreach ($tiers_won as $tier) {
	echo "User won $tier% of the winnings with one of their tickets <br>";
}
}
else echo "No winning ticket for this run in ANY tiers. <br>";
exit;
?>

 

 

Link to comment
Share on other sites

Ok, I took an array approach here.  I'll leave working out when all the winnings have been depleted up to you.  For example, you can't have 5 users win the second tier and 3 users win the third tier - because that would total 210% of the winnings (i.e. you're bankrupt).

 

<?php
$numbers_chosen = "8|18|3,18|8|3,8|18|20,8|30|3";
$winning_numbers = "8|18|3";

// Let's get those winning numbers into an array
$jackpot = explode('|', $winning_numbers);

$tiers_won = array();
$tickets = explode(',', $numbers_chosen);
foreach ($tickets as $ticket) {
$balls = explode('|', $ticket);
if ($balls == $jackpot) {
	// All three balls match the jackpot (in the right order too)
	$tiers_won[] = 100;		
}
elseif ( ! array_diff($balls, $jackpot)) {
	// All the balls are the same but not necessarily in the correct order.
	$tiers_won[] = 30;		
}
elseif ($balls[0] == $jackpot[0] AND $balls[1] == $jackpot[1]) {
	// The first two balls match those of the jackpot
	$tiers_won[] = 20;		
}
elseif (count(array_diff($balls, $jackpot)) == 1) {
	// Only one ball is a mismatch (not considering the order)
	$tiers_won[] = 5;		
}
}

if ($tiers_won) {
foreach ($tiers_won as $tier) {
	echo "User won $tier% of the winnings with one of their tickets <br>";
}
}
else echo "No winning ticket for this run in ANY tiers. <br>";
exit;
?>

 

i'll get back to you once i got all my stuff done with this, gorgeous man.

 

This lottery system is going to be beast.

 

 

Im going to add all my queries/etc so people get the gold added to there account/ that part is very easy, thanks abunch man, appreciate it.

 

I'll pm u my final code in a couple days if u're curious :)

Link to comment
Share on other sites

Update:

 

$numbers_chosen = "{$ticket['numbers_chosed']}";
$winning_numbers = "{$lotto['correct_balls']}";
// Let's get those winning numbers into an array
$jackpot = explode('|', $winning_numbers);
$tiers_won = array();
$tickets = explode(',', $numbers_chosen);
foreach ($tickets as $ticket) {
$balls = explode('|', $ticket);
if ($balls == $jackpot) {
	// All three balls match the jackpot (in the right order too)
	$tiers_won[] = 100;		
	$jackpotwon = True;
	$numbersthatwonjackpot = "{$balls['0']}|{$balls['1']}|{$balls['2']}";
	$userthatone = $ticket['name'];


}
elseif ( ! array_diff($balls, $jackpot)) {
	// All the balls are the same but not necessarily in the correct order.
	$tiers_won[] = 30;			
}
elseif ($balls[0] == $jackpot[0] AND $balls[1] == $jackpot[1]) {
	// The first two balls match those of the jackpot
	$tiers_won[] = 20;		
}
elseif (count(array_diff($balls, $jackpot)) == 1) {
	// Only one ball is a mismatch (not considering the order)
	$tiers_won[] = 5;		
}
}

if ($tiers_won) {
foreach ($tiers_won as $tier) {
	echo "User won $tier% of the winnings with one of their tickets <br>";
}
}
else echo "No winning ticket for this run in ANY tiers. <br>";

 

As you can see I added some variables in there.

 

$numbersthatwonjackpot = "{$balls['0']}|{$balls['1']}|{$balls['2']}";
	$userthatone = $ticket['name'];

 

The problem is, this is run in a while loop (code above)

 

$query = $DB->query("SELECT l.*,m.name,m.id FROM ibf_lottery_tickets l
							LEFT JOIN ibf_members m ON (m.id=l.memberid)
							WHERE l.lotteryid='{$lotto['i_id']}' ORDER BY time_bought ASC");
			$tickets_bought = $DB->get_num_rows();
			while($ticket = $DB->fetch_row($query)) {

Shouldn't

$ticket['name']

pull the "name" from my ibf_members table? If so it's not, and it act's like a Array? Is this because it's in the foreach loop?

 

If so, can you help me on getting the "name" table from the ibf_members into my

$ticket['name']

field?  I have a feeling multidimensional array is what I will need to use?

 

Thank you

 

Then I added at buttom (Which I will use to run queries to give the member gold, but this is not in any loop for obvious reasons

if ($jackpotwon){
				echo " $numbersthatwonjackpot Jackpot Won, User Who won the jackpot: $userthatone";
}

 

As you can see, $userthatone echo's out a array, I need it to get the name from ibf_members from that while loop, im stumped.

 

 

 

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.