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)?

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.

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

 

Also, if you used parentheses like so:

 

$pattern = "/^(ab|cd)([^xyz]*)xyz/";

 

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

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]*.

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.