PHP - 重新排列字符串

时间:2013-03-10 02:02:52

标签: php string

抱歉,标题可能不太正确,但我有点累了。

我想知道是否有人可以帮我解决问题,我有一个包含这个(或类似的,即遵循相同模式)文本的字符串。

文字:

玩家1($ 630) 玩家2($ 578) 点击($ 507) 玩家5(600美元) 球员6($ 621)

玩家1帖子(SB)3美元 球员2帖子(BB)$ 6

从声明stacksize的第一个文本中,您可以收集的唯一信息是哪个玩家具有哪个堆栈大小。

下面的信息告诉我们,玩家1是SB(小盲注),玩家2是BB(大盲注),根据这些信息,我现在可以推断出CLICKK是UTG,玩家4(在这种情况下不存在)是MP,玩家5是CO而玩家6是BTN。

始终遵循以下趋势:UTG在BB之后,MP在UTG之后,CO在MP之后,依此类推。

我想做的是将玩家名称,即玩家1,玩家2等替换为相应的位置,因此玩家1($ 630)将成为SB($ 630)等等。

然后最后我可以删除底部,因为它会变得多余。

我不是要求你为我做这件事,但如果你能指出我正确的方向,或者给我一些合乎逻辑的步骤,我会非常感激,谢谢。

1 个答案:

答案 0 :(得分:0)

据说,你必须在手牌开始时识别所有球员。可能正则表达式是字符串搜索的最佳方法,但我将尝试使用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会说明玩家'找到'的位置,并附加到新的字符串,或替换原始{ {1}} substr_replace

此外,当您收集玩家名称时,您可以在同一点收集堆栈大小,只需创建一个新的$Gamestring - 无论如何,现在您已将它们放在数组中,您可以使用它们做更多的事情。