Jump to content

Regex help?


ballhogjoni

Recommended Posts

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

Link to comment
https://forums.phpfreaks.com/topic/247435-regex-help/
Share on other sites

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'

Link to comment
https://forums.phpfreaks.com/topic/247435-regex-help/#findComment-1270752
Share on other sites

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

Link to comment
https://forums.phpfreaks.com/topic/247435-regex-help/#findComment-1270935
Share on other sites

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.