Jump to content

[SOLVED] Slight question on the regex functions, preg_match, etc...


kratsg

Recommended Posts

$data = file_get_contents('http://www.yahoo.com');
$pattern = "/<title>([^<]*)<\/title>/";
preg_match($pattern, $data, $matches);

 

How does the $matches part work? I understand it is AN array, but how do you know what goes into $matches[0], $matches[1]... $matches?

 

I'm a bit lost on that (and would it be the same for preg_match)?

Link to comment
Share on other sites

From the manual...

 

If matches is provided, then it is filled with the results of search. $matches[0] will contain the text that matched the full pattern, $matches[1] will have the text that matched the first captured parenthesized subpattern, and so on.
Link to comment
Share on other sites

What would be the text that matched the full pattern? o_o The <title>stuff</title> part?

 

Yes, the <title>stuff</title> part part from your last post.

 

Would $matches[1] contain the "ab" or the "cd" depending on the text?

 

$matches[1] would contain whatever matched ab|cd, $matches[2] would contain whatever matched [^xyz]*.

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.