Jump to content

Recommended Posts

I don't know much about writing PHP but something i was wondering and hoping someone could help me with.

 

An if statement that will swap text within [  ] to corresponding images stored on my server. I think an if statement will be the best way to do this as if the post doesn't contain the [ ] with text/numbers within it then the statement would be false and nothing would be done.

 

am i correct in my thinking here.

 

so basically if [2h 3c] then change 2h to 2h.jpg and 3c to 3c.jpg

and seeing how the text/number within are variables that cover all 52 playing cards and suites in a standard deck. i Have all image files and they are named for the card and suite. 

So 2h = 2 of hearts so on and so forth.

 

can anyone help me with this.

Link to comment
https://forums.phpfreaks.com/topic/185706-if-statements/
Share on other sites

You can use strpos() and str_replace to do something like this, very crude, but it might work

 

$images = "";
$start = strpos($text, "[");
$end = strpos($text, "]");
if($start && $end)
{
$images = str_replace(" ", " .jpg",  substr($text, $start, $end - $start);
$images .= ".jpg"
}
[code]
This assumes there's only one set of brackets and that everything is exactly in the form you provided. This will just give you the image names.

Link to comment
https://forums.phpfreaks.com/topic/185706-if-statements/#findComment-980606
Share on other sites

There is more than one set of brackets, but the format for them is the same.

 

there is something like

 

Full Tilt Poker Game #16783843176: Table Charger (deep) - $3/$6 - No Limit Hold'em - 22:07:32 ET - 2009/12/13

Seat 1: SadLittleDoggy ($998)

Seat 2: Amorpheous ($180)

Seat 3: novel20 ($1,257)

Seat 4: drillhead7619 ($326.55)

Seat 5: RobSainter ($894)

Seat 6: mjm32088 ($391)

Seat 7: leja2 ($807.90)

Seat 8: jeffr8 ($1,066.50)

Seat 9: HellaStacks_ ($810)

HellaStacks_ posts the small blind of $3

SadLittleDoggy is sitting out

Amorpheous posts the big blind of $6

The button is in seat #8

*** HOLE CARDS ***

Dealt to jeffr8 [Ac Ah]

SadLittleDoggy stands up

novel20 folds

drillhead7619 folds

RobSainter folds

mvisman adds $360

mjm32088 has 15 seconds left to act

mjm32088 folds

leja2 folds

jeffr8 raises to $16.50

HellaStacks_ has 15 seconds left to act

HellaStacks_ raises to $66

Amorpheous folds

jeffr8 calls $49.50

*** FLOP *** [As Qd Tc]

HellaStacks_ has 15 seconds left to act

HellaStacks_ bets $118

jeffr8 raises to $246

HellaStacks_ has 15 seconds left to act

HellaStacks_ calls $128

*** TURN *** [As Qd Tc] [Jc]

HellaStacks_ bets $498, and is all in

jeffr8 calls $498

HellaStacks_ shows [Ad Ks]

jeffr8 shows [Ac Ah]

*** RIVER *** [As Qd Tc Jc] [7h]

HellaStacks_ shows a straight, Ace high

jeffr8 shows three of a kind, Aces

HellaStacks_ wins the pot ($1,623) with a straight, Ace high

*** SUMMARY ***

Total pot $1,626 | Rake $3

Board: [As Qd Tc Jc 7h]

Seat 1: SadLittleDoggy is sitting out

Seat 2: Amorpheous (big blind) folded before the Flop

Seat 3: novel20 didn't bet (folded)

Seat 4: drillhead7619 didn't bet (folded)

Seat 5: RobSainter didn't bet (folded)

Seat 6: mjm32088 didn't bet (folded)

Seat 7: leja2 didn't bet (folded)

Seat 8: jeffr8 (button) showed [Ac Ah] and lost with three of a kind, Aces

Seat 9: HellaStacks_ (small blind) showed [Ad Ks] and won ($1,623) with a straight, Ace high

 

 

As you can see only the items within the brackets need to change.

 

Link to comment
https://forums.phpfreaks.com/topic/185706-if-statements/#findComment-980607
Share on other sites

I am sure there is a regular expression for this this is what I came up with

 

<?php
$string = 'Dealt to jeffr8 [2h 3g], Dealt to me [4g 3h]';


preg_match_all("#\[(.*?)\]#", $string, $matches);

foreach($matches[1] as $match) {
    $replace_string = preg_replace('#([0-9a-z]{2,2})#', "<img src='$1.jpg' />", $match);

    $string = str_replace("[$match]", $replace_string, $string);
}

print $string;
?>

Link to comment
https://forums.phpfreaks.com/topic/185706-if-statements/#findComment-981035
Share on other sites

Ok so i lost the entire message because of the editing limit.

 

Thanks SMF......... (should let you copy and paste ur message somewhere or let you post a reply instead...)

 

anyway;

 

Two options: (this will be blunt because im slightly aggravated, but should help anyways);

 

1. Use preg_replace, with a special syntax you can do this on a single line of code. Otherwise;

2. Use my Method described below:

 

:Concept:

1. Use Preg_Match_All() to get every bracket and put it into an array.

2. Save the array as the original (used later to replace the originals).

3. Loop through each original array item whilst:

4. (per item) remove left and right bracket from the string;

5. (per item) explode the string by the space character (Creates an array of strings that were seperated by the space char);

6. (per item) loop each new string item from the new array and change it into an image (save them into a temp variable);

7. (per item) add the brackets on the front and back of the new string to the temp variable.

8. add the temp variable to a new array

9. use str_replace with the original and new arrays to replace each bracket one at a time.

 

Voila!.

 

Make some code and i'll help you with it (i have written the code but u must learn so go write something!) ;D

 

Hope this Helps

-CB-

Link to comment
https://forums.phpfreaks.com/topic/185706-if-statements/#findComment-981038
Share on other sites

I'll give you the code even though it's a little more bulky than rajivgonsalves:

 


<?php

$text = "Full Tilt Poker Game #16783843176: Table Charger (deep) - $3/$6 - No Limit Hold'em - 22:07:32 ET - 2009/12/13
Seat 1: SadLittleDoggy ($998)
Seat 2: Amorpheous ($180)
Seat 3: novel20 ($1,257)
Seat 4: drillhead7619 ($326.55)
Seat 5: RobSainter ($894)
Seat 6: mjm32088 ($391)
Seat 7: leja2 ($807.90)
Seat 8: jeffr8 ($1,066.50)
Seat 9: HellaStacks_ ($810)
HellaStacks_ posts the small blind of $3
SadLittleDoggy is sitting out
Amorpheous posts the big blind of $6
The button is in seat #8
*** HOLE CARDS ***
Dealt to jeffr8 [Ac Ah]
SadLittleDoggy stands up
novel20 folds
drillhead7619 folds
RobSainter folds
mvisman adds $360
mjm32088 has 15 seconds left to act
mjm32088 folds
leja2 folds
jeffr8 raises to $16.50
HellaStacks_ has 15 seconds left to act
HellaStacks_ raises to $66
Amorpheous folds
jeffr8 calls $49.50
*** FLOP *** [As Qd Tc]
HellaStacks_ has 15 seconds left to act
HellaStacks_ bets $118
jeffr8 raises to $246
HellaStacks_ has 15 seconds left to act
HellaStacks_ calls $128
*** TURN *** [As Qd Tc] [Jc]
HellaStacks_ bets $498, and is all in
jeffr8 calls $498
HellaStacks_ shows [Ad Ks]
jeffr8 shows [Ac Ah]
*** RIVER *** [As Qd Tc Jc] [7h]
HellaStacks_ shows a straight, Ace high
jeffr8 shows three of a kind, Aces
HellaStacks_ wins the pot ($1,623) with a straight, Ace high
*** SUMMARY ***
Total pot $1,626 | Rake $3
Board: [As Qd Tc Jc 7h]
Seat 1: SadLittleDoggy is sitting out
Seat 2: Amorpheous (big blind) folded before the Flop
Seat 3: novel20 didn't bet (folded)
Seat 4: drillhead7619 didn't bet (folded)
Seat 5: RobSainter didn't bet (folded)
Seat 6: mjm32088 didn't bet (folded)
Seat 7: leja2 didn't bet (folded)
Seat 8: jeffr8 (button) showed [Ac Ah] and lost with three of a kind, Aces
Seat 9: HellaStacks_ (small blind) showed [Ad Ks] and won ($1,623) with a straight, Ace high";

preg_match_all("/\[[a-z ]+\]/i",$text,$matches);
// returns a multi dimensional array.
/* EG:
array{
  [0]=> array{
	[0]=> "[Ac Ah]"
	[1]=> "[As Qd Tc]"
	[2]=> "[As Qd Tc]"
	[3]=> "[Jc]"
	[4]=> "[Ad Ks]"
	[5]=> "[Ac Ah]"
	[6]=> "[As Qd Tc Jc]"
	[7]=> "[Ac Ah]"
	[8]=> "[Ad Ks]"
}
}


*/

// Declare variables so we dont get notice errors;
$replace_strings = $matches[0]; // These are the original strings.
$replacement_strings = array(); // Will be replaced with these strings.

// loop each bracket;
foreach($matches[0] as $bracket){

// Get rid of the brackets.
$bracket = substr($bracket,1,strlen($bracket) - 2);

// Get each individual character string
$this_str_array = explode(" ",$bracket);

// Check if it's an array
if(!is_array($this_str_array)){ // To be honest this shouldnt happen
	$temp_string = "[".$bracket.".jpg]"; // Add the replacement string
}else{
	// Start with a bracket
	$temp_string = "[";

	// Add each image
	foreach($this_str_array As $chars){
		$temp_string .= $chars.".jpg";
	}

	// End with a Bracket
	$temp_string .= "]";
}
// Add Result
$replacement_strings[] = $temp_string; 
}

$text = str_replace($replace_strings,$replacement_strings,$text);

echo($text);

 

Maybe you'll learn something from this one too; With all the comments.

 

-CB-

Link to comment
https://forums.phpfreaks.com/topic/185706-if-statements/#findComment-981041
Share on other sites

you could try this out you'll have to adjust the proper path of your image directory

 

<?php
$string = "Full Tilt Poker Game #16783843176: Table Charger (deep) - $3/$6 - No Limit Hold'em - 22:07:32 ET - 2009/12/13
Seat 1: SadLittleDoggy ($998)
Seat 2: Amorpheous ($180)
Seat 3: novel20 ($1,257)
Seat 4: drillhead7619 ($326.55)
Seat 5: RobSainter ($894)
Seat 6: mjm32088 ($391)
Seat 7: leja2 ($807.90)
Seat 8: jeffr8 ($1,066.50)
Seat 9: HellaStacks_ ($810)
HellaStacks_ posts the small blind of $3
SadLittleDoggy is sitting out
Amorpheous posts the big blind of $6
The button is in seat #8
*** HOLE CARDS ***
Dealt to jeffr8 [Ac Ah]
SadLittleDoggy stands up
novel20 folds
drillhead7619 folds
RobSainter folds
mvisman adds $360
mjm32088 has 15 seconds left to act
mjm32088 folds
leja2 folds
jeffr8 raises to $16.50
HellaStacks_ has 15 seconds left to act
HellaStacks_ raises to $66
Amorpheous folds
jeffr8 calls $49.50
*** FLOP *** [As Qd Tc]
HellaStacks_ has 15 seconds left to act
HellaStacks_ bets $118
jeffr8 raises to $246
HellaStacks_ has 15 seconds left to act
HellaStacks_ calls $128
*** TURN *** [As Qd Tc] [Jc]
HellaStacks_ bets $498, and is all in
jeffr8 calls $498
HellaStacks_ shows [Ad Ks]
jeffr8 shows [Ac Ah]
*** RIVER *** [As Qd Tc Jc] [7h]
HellaStacks_ shows a straight, Ace high
jeffr8 shows three of a kind, Aces
HellaStacks_ wins the pot ($1,623) with a straight, Ace high
*** SUMMARY ***
Total pot $1,626 | Rake $3
Board: [As Qd Tc Jc 7h]
Seat 1: SadLittleDoggy is sitting out
Seat 2: Amorpheous (big blind) folded before the Flop
Seat 3: novel20 didn't bet (folded)
Seat 4: drillhead7619 didn't bet (folded)
Seat 5: RobSainter didn't bet (folded)
Seat 6: mjm32088 didn't bet (folded)
Seat 7: leja2 didn't bet (folded)
Seat 8: jeffr8 (button) showed [Ac Ah] and lost with three of a kind, Aces
Seat 9: HellaStacks_ (small blind) showed [Ad Ks] and won ($1,623) with a straight, Ace high
";


$string = preg_replace_callback("#\[{1,1}(.*?)\]{1,1}#", create_function('$match', 'return preg_replace(\'#([0-9A-Za-z]{2,2})#\', "<img src=\'$1.jpg\' />", $match[1]);'), $string);

echo $string;

?>

Link to comment
https://forums.phpfreaks.com/topic/185706-if-statements/#findComment-981097
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.