ultrus Posted July 19, 2011 Share Posted July 19, 2011 I'm having a brain lock on this one. Any thoughts on how to get "526" in "blah blah REFERENCE ID: [526] blah blah"? Thanks a ton in advance. Quote Link to comment https://forums.phpfreaks.com/topic/242364-getting-the-number-in-blah-blah-reference-id-526-blah-blah/ Share on other sites More sharing options...
AyKay47 Posted July 19, 2011 Share Posted July 19, 2011 $subject = "blah blah REFERENCE ID: [526] blah blah"; $pattern = '~^.*\[(\d+)\].*$~is'; preg_match($pattern, $subject, $matches); print $matches[0]; // print 526 Quote Link to comment https://forums.phpfreaks.com/topic/242364-getting-the-number-in-blah-blah-reference-id-526-blah-blah/#findComment-1244812 Share on other sites More sharing options...
ultrus Posted July 19, 2011 Author Share Posted July 19, 2011 You are the best! Thank you for sanity. Quote Link to comment https://forums.phpfreaks.com/topic/242364-getting-the-number-in-blah-blah-reference-id-526-blah-blah/#findComment-1244820 Share on other sites More sharing options...
AyKay47 Posted July 19, 2011 Share Posted July 19, 2011 You are the best! Thank you for sanity. no problem man.. please mark as solved.. Quote Link to comment https://forums.phpfreaks.com/topic/242364-getting-the-number-in-blah-blah-reference-id-526-blah-blah/#findComment-1244828 Share on other sites More sharing options...
ultrus Posted July 19, 2011 Author Share Posted July 19, 2011 Sorry was on cell phone, get's difficult. Consider this solved. Quote Link to comment https://forums.phpfreaks.com/topic/242364-getting-the-number-in-blah-blah-reference-id-526-blah-blah/#findComment-1244859 Share on other sites More sharing options...
.josh Posted July 19, 2011 Share Posted July 19, 2011 $subject = "blah blah REFERENCE ID: [526] blah blah"; $pattern = '~^.*\[(\d+)\].*$~is'; preg_match($pattern, $subject, $matches); print $matches[0]; // print 526 I don't think I agree with this approach. Firstly, $matches[0] will contain the full pattern match. According to your regex, you should be looking at $matches[1]. Also, there's really no need for a lot of that stuff.. like the anchor tags, the last .*, or those modifiers, based on your regex. Also, if you're just going to assume there's no other numbers in $subject, no need even for the first .* IOW based on your assumptions, You could just do '~\[(\d+)~'. Also \d technically matches more than straight numbers... But on that note..more importantly, that pattern is going to grab the first set of numbers preceded by a [ found in $subject, so it's not going to return the desired number if for instance $subject = "blah [123] more blah REFERENCE ID: [526] blah blah";. But I blame the OP for not being more specific with examples of what he's trying to get the number from. I think at a minimum, a pattern more like this should be used: ~REFERENCE ID: \[\K[0-9]+~ and then look at $matches[0] but..I think it would be "safer" if the OP were to give more details about he $subject he's trying to parse, actually show some real example(s). Quote Link to comment https://forums.phpfreaks.com/topic/242364-getting-the-number-in-blah-blah-reference-id-526-blah-blah/#findComment-1244916 Share on other sites More sharing options...
AyKay47 Posted July 20, 2011 Share Posted July 20, 2011 $subject = "blah blah REFERENCE ID: [526] blah blah"; $pattern = '~^.*\[(\d+)\].*$~is'; preg_match($pattern, $subject, $matches); print $matches[0]; // print 526 I don't think I agree with this approach. Firstly, $matches[0] will contain the full pattern match. According to your regex, you should be looking at $matches[1]. Also, there's really no need for a lot of that stuff.. like the anchor tags, the last .*, or those modifiers, based on your regex. Also, if you're just going to assume there's no other numbers in $subject, no need even for the first .* IOW based on your assumptions, You could just do '~\[(\d+)~'. Also \d technically matches more than straight numbers... But on that note..more importantly, that pattern is going to grab the first set of numbers preceded by a [ found in $subject, so it's not going to return the desired number if for instance $subject = "blah [123] more blah REFERENCE ID: [526] blah blah";. But I blame the OP for not being more specific with examples of what he's trying to get the number from. I think at a minimum, a pattern more like this should be used: ~REFERENCE ID: \[\K[0-9]+~ and then look at $matches[0] but..I think it would be "safer" if the OP were to give more details about he $subject he's trying to parse, actually show some real example(s). I agree with you on this CV, there are definitely better ways of approaching this...but from the OP's vague question and poor example...I threw something together quick that would work for him. If he had given me a better example, I would have created a more precise regex here...however thank you for pointing this matter out to both he OP and me Quote Link to comment https://forums.phpfreaks.com/topic/242364-getting-the-number-in-blah-blah-reference-id-526-blah-blah/#findComment-1244964 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.