假设,您必须在牌局开始时识别所有玩家。可能正则表达式是字符串搜索的最佳方法,但我会尝试使用 PHP Explode:
<?php
/**
* Input Strings
**/
$GameString = "Player 1 ($630) Player 2 ($578) CLICKK
($507) Player 5 ($600) Player 6 ($621)";
$PostBlindString = "Player 1 posts (SB) $3 Player 2 posts (BB) $6";
$data = explode( ")", $GameString );
$players = array();
/**
* Get the Small Blind Player Name
**/
$SmallBlindPlayer = trim(
substr( $PostBlindString, 0,
strrpos( $PostBlindString, " posts (SB)"
)
)
);
echo $SmallBlindPlayer;
// (Echos 'Player 1' )
/**
* Go through each exploded string
* find it's name before the bracket
**/
foreach ( $data as $p ) {
if ( $p !== '' )
$players[] = trim(substr( $p, 0, strrpos( $p, "(" )));
}
/**
* Our Resulting players
**/
print_r( $players );
Array
(
[0] => Player 1
[1] => Player 2
[2] => CLICKK
[3] => Player 5
[4] => Player 6
)
/**
* Game states array
* when we know someone's
* position, we can assign it
* through some loop
**/
$gameStates = array (
"SB",
"BB",
"UTG",
"MP",
"CO",
"BTN"
);
/**
* Find the small button player
**/
for ( $x = 0; $x < count($players); $x++ ) {
if ( $players[$x] == $SmallBlindPlayer )
echo $players[$x] . " This player is the small blind!";
}
/**
* Go through them, as assign it
* loop back to start if the player
* is late in the array
**/
$PlayerPositions = array();
$LoopedThrough = false;
$Found = false;
$FoundAt = 0;
$c = 0;
for ( $x = 0; $x < count($players); $x++ ) {
if ( $players[$x] == $SmallBlindPlayer && !$Found ) {
$PlayerPositions[$players[$x]] = $gameStates[$c];
$Found = true;
$FoundAt = $x;
} else {
if ( $Found ) {
if ( $x != $FoundAt )
$PlayerPositions[$players[$x]] = $gameStates[$c++];
}
if ( $Found && !$LoopedThrough ) {
$x = -1; $LoopedThrough = true;
}
}
}
/**
* Print the "merged" arrays
**/
print_r( $PlayerPositions );
Array
(
[Player 1] => SB
[Player 2] => BB
[CLICKK] => UTG
[Player 5] => MP
[Player 6] => CO
)
?>
我的想法是,从这里开始,您可以遍历玩家列表,知道您$Gamestates
将说明您的玩家“找到”的位置,并附加到一个新字符串,或者用$Gamestring
类似substr_replace的东西替换原始字符串
此外,您可以在收集玩家姓名的同时收集堆栈大小以简单地创建一个新玩家名称$GameString
- 无论如何,现在您将它们放在数组中,您可以使用它们做更多事情。