opencombatclan Posted December 13, 2009 Share Posted December 13, 2009 Hello there, Here I am once again. I have got a little different problem now. I tried some preg matching myself, but it didn't workout properly (I am not familiar with them) Here is a string that I got 2 16 142 0 ^5^1>>^4BLAbla^1<<^0Blaaah^7 50 50 123.12.12.12:-12334 7629 5000 Now I need to get the following variables: $name and $ip (I don't need any other one's) Now here are the difficulties 1. The amount of white-spaces between the values is random (/s+) 2. The name itself COULD contain (multiple) white-spaces and almost any character (including special ones) 3. The ip port (:number) could be positive or negative (:-number) Here are some constants: 1. Values are always seperated by at least 1 white-space 2. Name always ends with ^7 Here is another string (for reference): 5 61 82 jsdJSBCSS72hD67238sDSS 1|^9Bla hh^113 |*^9Bl aBla^1! ^7 0 23.233.233.23:22220 2057 25000 I hope someone could help me out Once again I tried my best to make my idea clear Legenda: Lime green = always a number Blue = number or alphanumeric (long or short) EDIT: dang! At the moment I pressed 'submit' I realized that I am (once again) In the wrong forum (should be in php regex forum), SRRY! Quote Link to comment https://forums.phpfreaks.com/topic/184994-another-template-match/ Share on other sites More sharing options...
opencombatclan Posted December 13, 2009 Author Share Posted December 13, 2009 I am so ashamed... I forgot another thinggy... I also need the FIRST number of these strings... (string1: 2 and string2: 5).... =S Quote Link to comment https://forums.phpfreaks.com/topic/184994-another-template-match/#findComment-976560 Share on other sites More sharing options...
cags Posted December 13, 2009 Share Posted December 13, 2009 Attempt #1 $pattern = '#^[0-9]+\s+[0-9]+\s+[0-9]+\s+[a-z0-9]+\s+(.+?)\^7\s+[0-9]+\s+([0-9.:-]+)#i'; $input = '5 61 82 jsdJSBCSS72hD67238sDSS 1|^9Bla hh^113 |*^9Bl aBla^1! ^7 0 23.233.233.23:22220 2057 25000'; echo '<pre>'; preg_match($pattern, $input, $out); Quote Link to comment https://forums.phpfreaks.com/topic/184994-another-template-match/#findComment-976566 Share on other sites More sharing options...
opencombatclan Posted December 13, 2009 Author Share Posted December 13, 2009 It works for the second string. However it returns an empty array If I try it with the first string... (I fixed that by putting \s+ in front); And is it also possible to get the first number of the strings? Anyhow, thnx for your help so far! Quote Link to comment https://forums.phpfreaks.com/topic/184994-another-template-match/#findComment-976589 Share on other sites More sharing options...
cags Posted December 13, 2009 Share Posted December 13, 2009 Your first and second string actually followed different patterns, I guessed as to which one was correct. Your first pattern is ^7 num num ip whereas your second was ^7 num ip. To fetch the first number you just put brackets around the first [0-9]+. Quote Link to comment https://forums.phpfreaks.com/topic/184994-another-template-match/#findComment-976591 Share on other sites More sharing options...
opencombatclan Posted December 13, 2009 Author Share Posted December 13, 2009 You are great! I learned a lot from your expression! Its solved now. I just added \s+ in front of your expression and I putted brackets arround the first [0-9]+ Thank you very very very much!!! Quote Link to comment https://forums.phpfreaks.com/topic/184994-another-template-match/#findComment-976594 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.