kratsg Posted October 23, 2007 Share Posted October 23, 2007 $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 https://forums.phpfreaks.com/topic/74388-solved-slight-question-on-the-regex-functions-preg_match-etc/ Share on other sites More sharing options...
trq Posted October 23, 2007 Share Posted October 23, 2007 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 https://forums.phpfreaks.com/topic/74388-solved-slight-question-on-the-regex-functions-preg_match-etc/#findComment-375851 Share on other sites More sharing options...
kratsg Posted October 23, 2007 Author Share Posted October 23, 2007 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? Link to comment https://forums.phpfreaks.com/topic/74388-solved-slight-question-on-the-regex-functions-preg_match-etc/#findComment-375857 Share on other sites More sharing options...
trq Posted October 23, 2007 Share Posted October 23, 2007 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 https://forums.phpfreaks.com/topic/74388-solved-slight-question-on-the-regex-functions-preg_match-etc/#findComment-375862 Share on other sites More sharing options...
kratsg Posted October 23, 2007 Author Share Posted October 23, 2007 Now I finally understand... and I see teh LIGHT! :-o Link to comment https://forums.phpfreaks.com/topic/74388-solved-slight-question-on-the-regex-functions-preg_match-etc/#findComment-375863 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.