ballhogjoni Posted September 19, 2011 Share Posted September 19, 2011 I have this regex /http:\/\/(.*?)\/.*?\?.*?&?(?|q|qt)=([^&=]{2,}?)(?:&|$)/ I am trying to get the keyword from the url like http://search.yahoo.com/search;_ylt=A0WTfYQKqWlO.FYA2CibvZx4?fr=yfp-t-701-s&toggle=1&cop=mss&ei=UTF8&p=keyword my problem is that its getting the value for the key cop. I need to get the value of the key p. Any ideas? thanks Quote Link to comment Share on other sites More sharing options...
AyKay47 Posted September 19, 2011 Share Posted September 19, 2011 <?php $pattern = "~http:.*&p=([0-9a-zA-Z]+)~i"; $string = "http://search.yahoo.com/search;_ylt=A0WTfYQKqWlO.FYA2CibvZx4?fr=yfp-t-701-s&toggle=1&cop=mss&ei=UTF8&p=keyword"; preg_match($pattern,$string,$matches); print $matches[1]; ?> Quote Link to comment Share on other sites More sharing options...
ballhogjoni Posted September 19, 2011 Author Share Posted September 19, 2011 thanks but the key can be either p, q or qt or a combination of the three. Quote Link to comment Share on other sites More sharing options...
ballhogjoni Posted September 19, 2011 Author Share Posted September 19, 2011 also you took out all the matches i need Quote Link to comment Share on other sites More sharing options...
AyKay47 Posted September 19, 2011 Share Posted September 19, 2011 you said you want to match the p key... so thats what I gave you.. perhaps you should explain better.. Quote Link to comment Share on other sites More sharing options...
ballhogjoni Posted September 19, 2011 Author Share Posted September 19, 2011 figured it out. This is what I needed. /http:\/\/(.*?)\/.*?(?:[&?]p|q|qt)=([^&=]{2,}?)(?:&|$)/ Quote Link to comment Share on other sites More sharing options...
.josh Posted September 19, 2011 Share Posted September 19, 2011 There are other functions that specifically handle parsing URL strings; here is a non-regex approach. I'm not hatin' on regex (I love regex) but you should avoid using regex when possible, because most people aren't that great with regex, and so it makes it that much harder to maintain or expand code. // url to parse $url = "http://search.yahoo.com/search;_ylt=A0WTfYQKqWlO.FYA2CibvZx4?fr=yfp-t-701-s&toggle=1&cop=mss&ei=UTF8&p=keyword"; // list of url params you want to look for. Code below will return value of first one found $param_list = array('p','q','qt'); // parse the URL and get its components $comp = parse_url($url); // get the host $host = $comp['host']; // parse the query string and return an associative array of all query string params parse_str($comp['query'],$params); // attempt to find value of one of the query string params from your list foreach ($param_list as $k) { if (array_key_exists($k,$params)) { // Your regex doesn't show that you care which param was found, // but if that need changes, then $k is the param that was found // assign the value of the found param to $keyword $keyword = $params[$k]; break; } } echo $host; // 'psearch.yahoo.com' echo $keyword; // 'keyword' Quote Link to comment Share on other sites More sharing options...
AyKay47 Posted September 20, 2011 Share Posted September 20, 2011 There are other functions that specifically handle parsing URL strings; here is a non-regex approach. I'm not hatin' on regex (I love regex) but you should avoid using regex when possible, because most people aren't that great with regex, and so it makes it that much harder to maintain or expand code. // url to parse $url = "http://search.yahoo.com/search;_ylt=A0WTfYQKqWlO.FYA2CibvZx4?fr=yfp-t-701-s&toggle=1&cop=mss&ei=UTF8&p=keyword"; // list of url params you want to look for. Code below will return value of first one found $param_list = array('p','q','qt'); // parse the URL and get its components $comp = parse_url($url); // get the host $host = $comp['host']; // parse the query string and return an associative array of all query string params parse_str($comp['query'],$params); // attempt to find value of one of the query string params from your list foreach ($param_list as $k) { if (array_key_exists($k,$params)) { // Your regex doesn't show that you care which param was found, // but if that need changes, then $k is the param that was found // assign the value of the found param to $keyword $keyword = $params[$k]; break; } } echo $host; // 'psearch.yahoo.com' echo $keyword; // 'keyword' nice.. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.