Jump to content

PHP Regex + Grouping


razta

Recommended Posts

Hello,

I am trying to match a regex and extract the data from the matched string that I want.

 

I want to extract the Apache version only from the Apache website.

 

So I am using the following raw sting to do the match:

<li><a href="#apache22">2.2.15</a> (released 2010-03-06)</li>

 

And the data I want from the above raw string is just the '2.2.15'.

 

Here is the code related to this problem:

// Match regex in grabbed HTML source
preg_match('/<li><a\shref="#apache22">(\d\.\d\.\d?\d)<\/a>\s\(released\s\d\d\d\d-\d\d-\d\d\)<\/li>/', $grabPage, $regex_version);

echo $regex_version[0];

 

I expected the above to output '2.2.15' instead it output '<li><a href="#apache22">2.2.15</a> (released 2010-03-06)</li>'.

 

Any help appreciated. Thanks in advance.

Link to comment
Share on other sites

Capture group 0 always returns whatever string matched the entire pattern, therefore that is what is located in $regex_version[0]. The version number 2.2.15 should be contained within $regex_version[1].

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.