tilt Posted August 7, 2009 Share Posted August 7, 2009 Im trying to replace a few words in a text. $convert = " Full Tilt Poker Game #13864648535: $110 + $5 Heads Up Sit & Go (102264740), Table 1 - 15/30 - No Limit Hold\'em - 21:01:44 ET - 2009/08/06 Seat 1: Oneword (1,785) Seat 2: Two Words (1,215) Oneword posts the small blind of 15 Two Words posts the big blind of 30 The button is in seat #1 *** HOLE CARDS *** Dealt to Oneword [6s 8s] Oneword raises to 60 Two Words raises to 210 Oneword calls 150 *** FLOP *** [7s 9s 7c] Two Words bets 270 Oneword raises to 1,575, and is all in Two Words calls 735, and is all in Oneword shows [6s 8s] Two Words shows [Ad 2s] Uncalled bet of 570 returned to Oneword *** TURN *** [7s 9s 7c] [Qs] *** RIVER *** [7s 9s 7c Qs] [5s] Oneword shows a straight flush, Nine high "; I need to determin who is in seat #1 to #10. The names between : and ( could be anything. But they're always between : and ( so I need some help with a synthax to determine that in vars like: $seat1 = $seat2 = $seat3 = $seat4 = $seat5 = $seat6 = $seat7 = $seat8 = $seat9 = $seat10 = There not always 10 seats. I need to fill the seats till how many seats there are. Could some1 point me in the right direction? What synthax can I use to determine the above? Link to comment https://forums.phpfreaks.com/topic/169202-need-help-converting-a-tekst/ Share on other sites More sharing options...
JonnoTheDev Posted August 7, 2009 Share Posted August 7, 2009 Add the following under your $convert string <?php preg_match_all('/Seat ([0-9]+):\\ (.*)\\ \\(/', $convert, $result, PREG_PATTERN_ORDER); $seats = $result[1]; $players = $result[2]; for($x = 0; $x < count($result[0]); $x++) { print "Seat ".$seats[$x].": ".$players[$x]."<br />"; } ?> Link to comment https://forums.phpfreaks.com/topic/169202-need-help-converting-a-tekst/#findComment-892778 Share on other sites More sharing options...
tilt Posted August 7, 2009 Author Share Posted August 7, 2009 Wow thanks for the fast reply. It works. I have the seats & players in vars now. Now I've got another question. I need $button to have the number at the end of this line The button is in seat #1 $button = 1 And I need $mainplayer to have the name in this line: Dealt to Oneword [6s 8s] $mainplayer = Oneword the name is always in between "to" and "[". And this line will be in $convert always one time. Should this be done with the same syntax? Or is it easy-er with some other synthax? I appreciate that u provided me with a piece of code (which I dont fully understand(yet)). I would be also happy if I was pushed in the right direction, im not lazy to google. I just dont know where to start searching :/ Link to comment https://forums.phpfreaks.com/topic/169202-need-help-converting-a-tekst/#findComment-892786 Share on other sites More sharing options...
JonnoTheDev Posted August 7, 2009 Share Posted August 7, 2009 Ok, it is all done using regular expression pattern matching preg_match_all('/Seat ([0-9]+):\\ (.*)\\ \\(/' Everything you see within () is known as a backreference i.e. ([0-9]+) (.*) and captures that piece of data. My advice is get a copy of regexbuddy. This will allow you to build and test regular expressions before putting into your code. It is an essential tool for programmers. http://www.regexbuddy.com/ Link to comment https://forums.phpfreaks.com/topic/169202-need-help-converting-a-tekst/#findComment-892797 Share on other sites More sharing options...
tilt Posted August 7, 2009 Author Share Posted August 7, 2009 Thanks. Can u explain what the : and / and \ and \\ and (.*) do in: preg_match_all('/Seat ([0-9]+):\\ (.*)\\ \\(/', $convert, $result, PREG_PATTERN_ORDER); If I understand correctly I can make something like preg_match_all('The button is in seat #\\ (.*)\\ \\/', $convert, $result, PREG_PATTERN_ORDER); (not sure because I dont know what all the slashes do) and preg_match_all('Dealt to \\ (.*)\\ \\[/', $convert, $result, PREG_PATTERN_ORDER); And I dont need a For construction because there's always only 1 line. The word I need will be in $result..Am i right? Link to comment https://forums.phpfreaks.com/topic/169202-need-help-converting-a-tekst/#findComment-892825 Share on other sites More sharing options...
JonnoTheDev Posted August 7, 2009 Share Posted August 7, 2009 / is an indicator that there is a regular expression between both start and end / \\ are escape characters (same as escaping quotes in strings) for escaping special characters .* matches any character except a line break () create a back reference to capture the portion of the string in the returned array Try your expressions out. This is why I highly recommend Regexbuddy as you can test them out on your string rather than modifying them in your source code if incorrect. Link to comment https://forums.phpfreaks.com/topic/169202-need-help-converting-a-tekst/#findComment-892827 Share on other sites More sharing options...
tilt Posted August 7, 2009 Author Share Posted August 7, 2009 Thanks alot for your help I figured it out, it works. Can U confirm if these are the right ways?: preg_match_all('/Dealt to\\ (.*)\\ \\[/', $convert, $result2, PREG_PATTERN_ORDER); $hero = $result2[1]; for($x = 0; $x < count($result2[0]); $x++) { print "$hero[$x]"; } preg_match_all('/The button is in seat #(.*)/', $convert, $result3, PREG_PATTERN_ORDER); $button = $result3[1]; for($x = 0; $x < count($result3[0]); $x++) { print "$button[$x]"; } Link to comment https://forums.phpfreaks.com/topic/169202-need-help-converting-a-tekst/#findComment-892843 Share on other sites More sharing options...
JonnoTheDev Posted August 7, 2009 Share Posted August 7, 2009 The button is in seat #(.*) Don't use * for everything. If a number is expected then use ([0-9]+) Link to comment https://forums.phpfreaks.com/topic/169202-need-help-converting-a-tekst/#findComment-892862 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.