Asheeown Posted October 12, 2008 Share Posted October 12, 2008 A concept I never have gotten, when I look at example scripts that use preg functions and look for "p@$[2!*@]" (Made Up) I can never understand it. I need to decipher text coming in through a telnet connection, it's organized but I still can't seem to work it out, let me give an example: This is an example of what would come in, the directives are changed for confusion purposes: /status Status Text --- [Directive 1] Test message [Directive 1] [sub Directive 1]: Test message [Directive 1] [sub Directive 1] [sub Sub Directive 1]: Test message [Directive 2] Something for directive 2 [Directive 3] Something for directive 3 /status Most Recent Status --- Now what happens in this telnet session is the following: 1. Server connects to client 2. Server sends command /status 3. The first time everything from /status to the --- is pulled 4. While the session goes along those directives are sent to the server, just little bits of information, when the server is ready to pull again and store all the information received it will send the status command again, everything from the last --- to the new --- is taken and stripped 5. All directives and sub directives can be in an array the list isn't that long This is how it should come out: Directive 1 sent "Test message" Directive 1 sent "Test message" from Sub Directive 1 Directive 1 sent "Test message" from Sub Directive 1 by Sub Sub Directive 1 Directive 2 sent "Something for directive 2" Directive 3 sent "Something for directive 3" Status = Most recent status If anyone could help that would be great, a tutorial on text stripping would also be fantastic, all this information is just a test the real telnet session actually makes sense to the people who know what it's doing. Sorry for any confusion and just ask if you have any questions. Quote Link to comment Share on other sites More sharing options...
Asheeown Posted October 12, 2008 Author Share Posted October 12, 2008 I need to split my text and the category is in brackets [Category Name] Text How can I use the [] brackets in preg_match without it thinking I'm looking for a set of characters? Quote Link to comment Share on other sites More sharing options...
Zane Posted October 12, 2008 Share Posted October 12, 2008 you have to escape them with a backslash \[Category\sName\] would match [Category Name] Quote Link to comment Share on other sites More sharing options...
Asheeown Posted October 12, 2008 Author Share Posted October 12, 2008 If I use: <?php $Array = explode("\r\n", $Strip); foreach($Array as $v) { if (preg_match('/^(\[.+\])\s*(.+)\s*$/', $v, $reg)) { echo $reg[1] . " " . $reg[2]; echo "<br>"; } } ?> Just to split the string in half and separate it with a bunch of spaces it works for some [Directive 1] Test message But doesn't for others [Chat] Text: [in-Message Bracketed Text] . When text is enclosed in brackets further on in the text it will just separate the last character with the spaces, in this case the period. But it does work when brackets. [Chat] [username] says: test message Quote Link to comment Share on other sites More sharing options...
Asheeown Posted October 12, 2008 Author Share Posted October 12, 2008 Okay I'm so close, please can someone help me It looks like this now: if (preg_match('/([\[.+\]])\s+(.+)/', $v, $chars)) { echo("$chars[1] $chars[2]"); echo "<br>"; } It correctly splits every kind of directive, except the directive name is totally gone and only a closing bracket is shown, like below ] Connected on remote Should be Status Connected on remote Quote Link to comment Share on other sites More sharing options...
effigy Posted October 13, 2008 Share Posted October 13, 2008 Can you include a full sample of data since "Chat" and "Username" gave you trouble? Are you simply trying to remove the brackets, or add the text shown in your example as well? Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.