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