2013-03-10 87 views
0

对不起,标题可能不完全正确,但我有点累。PHP - 重新排列字符串

我想知道如果有人能帮我弄清楚什么,我有一个字符串包含这个(或类似的,即相同的模式)文本。

文本:

播放器1($ 630) 玩家2($ 578) CLICKK($ 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)等..

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

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

+0

你为什么要用这个字符串? – Ares 2013-03-10 02:24:50

+0

因为这是一个扑克手历史=)(从扑克客户端生成的文本文件..大概) – MackieeE 2013-03-10 02:26:36

+0

是的,就像MackieeE说的那样,它只是从一个纯文本文件中读取 – user1104652 2013-03-10 02:30:57

回答

0

假设您必须在开始时识别所有玩家。也许正则表达式是字符串搜索是最好的方法,但我会用PHP尝试爆炸:

<?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 - 无论现在你是否拥有阵列,你可以用它们做更多的事情。

+0

这真是太棒了,谢谢MackieeE,我会defo能够做到这一点那。非常感谢。 – user1104652 2013-03-10 14:25:48

+0

@ user1104652没问题! :)如果您觉得我已经为您做了充分的回答,请不要忘记标记为已回答,如果您有任何问题,请随时询问=]! – MackieeE 2013-03-10 14:35:55