PHP正则表达式并比较字符串中的变量 [英] PHP regex and compare variables in a string

查看:29
本文介绍了PHP正则表达式并比较字符串中的变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个棘手的问题,似乎我被卡住了.我有一个想法如何进行,但不知道如何在实践中做到.

i have a tricky problem and seems like i'm stuck. I have an idea how to proceed but no idea how to do it in practice.

我想要做的是将 .txt 文件中的字符串转换为另一种格式(使用正则表达式和变量?).主要问题是当我需要转换标有//comments 的那些行时.

What i want to do is convert a string inside .txt file to another format (using regex and variables?). The main problem is when i need to convert those lines marked with //comments.

注意:...villainx calls $x"在原始格式和应转换为的格式中的计算方式不同.这就是我需要一些认真帮助的问题.

NOTE: "...villainx calls $x" is calculated differently in original and in the format it should be converted to. And that's the problem i need some serious help.

示例:这个需要转换...

Example: This needs to be converted...

HERO posts small blind $0.50.
villain4 posts big blind $1.00.    
** Dealing down cards **
Dealt to HERO [  7s 8c 5d 8d ]
villain1 calls $1.00
villain2 raises to $3.00 // total sum a player raises to
villain3 calls $3.00
HERO calls $3.00
villain4 calls $3.00
villain1 calls $3.00     // total sum a player calls whether he has put money in to the pot before (as he has -- $1 call, first to act)
** Dealing Flop ** [ 9c, Ah, Jh ]

...至此:

HERO posts small blind [$0.50 USD].
villain4 posts big blind [$1.00 USD].    
** Dealing down cards **
Dealt to HERO [  7s 8c 5d 8d ]
villain1 calls [$1.00 USD]
villain2 raises [$3.00 USD] // total sum a player raises to
villain3 calls [$3.00 USD]
HERO calls [$2.50 USD]      // a sum player calls = last raise ($3) - money put in (=$0.50 small blind)
villain4 calls [$2.00 USD]  // $3 - $1 (big blind)
villain1 calls [$2.00 USD]  // $3 - $1 (the call first to act)
** Dealing Flop ** [ 9c, Ah, Jh ]

另一个例子:

HERO posts small blind $0.50.
villain4 posts big blind $1.00.    
** Dealing down cards **
Dealt to HERO [  7s 8c 5d 8d ]
villain1 bets $5.50
villain2 raises to $20.00
villain3 raises to $40.00
villain1 calls $40.00 //THIS NEEDS TO BE "calls $34.50"
villain2 calls $40.00 //THIS NEEDS TO BE "calls $20.00"
** Dealing Flop ** [ 9c, Ah, Jh ]

这是整只手应该是什么样子的完整示例.txt 文件可能包含数百手牌.我已经设法 preg_replace 基本上所有其他问题,但以上.我迷路了.请帮我!:D

and here's the full example how the whole hand should look. Txt file could contain a hundreds of hands. I've managed to preg_replace basically all other issues but that above. I'm lost. Please help me! :D

***** Hand History for Game 335502358 ***** (Full Tilt)
$100.00 USD PL Omaha - Thursday, October 15, 01:32:21 ET 2009
Table Foxtrot (Real Money)
Seat 3 is the button
Seat 1: villain1 ( $38.50 USD )
Seat 2: villain2 ( $99.65 USD )
Seat 3: villain3 ( $415.55 USD )
Seat 4: HERO ( $99.00 USD )
Seat 6: villain4 ( $171.20 USD )
HERO posts small blind [$0.50 USD].
villain4 posts big blind [$1.00 USD].
** Dealing down cards **
Dealt to HERO [  7s 8c 5d 8d ]
villain1 calls [$1.00 USD]
villain2 raises [$3.00 USD]
villain3 calls [$3.00 USD]
HERO calls [$2.50 USD]
villain4 calls [$2.00 USD]
villain1 calls [$2.00 USD]
** Dealing Flop ** [ 9c, Ah, Jh ]
HERO checks
villain4 checks
villain1 checks
villain2 bets [$8.00 USD]
villain3 folds
HERO folds
villain4 calls [$8.00 USD]
villain1 folds
** Dealing Turn ** [ Th ]
villain4 checks
villain2 bets [$13.00 USD]
villain4 calls [$13.00 USD]
** Dealing River ** [ 3c ]
villain4 checks
villain2 checks
villain2 shows [Qc, Js 8s Qd ]
villain4 shows [Kh, Tc 7h Kd ]
villain4 wins $54.15 USD from main pot

编辑 1:添加注释以澄清我的真正问题

edit 1: added NOTE to clarify my real question

编辑 2:添加另一个示例

edit 2: added another example

推荐答案

您能否使用 preg_match 取出美元值并使用 preg_replace 重新排列字符串?

Could you use a preg_match to pull out the dollar value and re-arrange the string with a preg_replace?

$regex = '/(\$[0-9.]+)/';

$matched = preg_match($regex, $stringToMatch, $matches);

if($matched > 0)
{
  $output string = preg_replace($regex, '['.$matches[0].' USD]', $stringToMatch);
}

唯一不会做的就是忽略开头声明每个座位"的行,因此您可能需要先过滤掉那些[简单的 strpos($stringToMatch, 'Seat') 可能就足够了,虽然不是很优雅].

The only thing this won't do is ignore the lines at the beginning where you declare each 'seat' so you might need to filter those out first [simple strpos($stringToMatch, 'Seat') might be enough there, not wonderfully elegant though].

这篇关于PHP正则表达式并比较字符串中的变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆