Jump to content

getting the number in "blah blah REFERENCE ID: [526] blah blah"


ultrus

Recommended Posts

$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).

Link to comment
Share on other sites

$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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.