Jump to content

[SOLVED] Regex to parse url


lovelldr

Recommended Posts

Hi guys.

 

I was looking to get a regular expression to parse a url to get some information...

 

Basically, on my website, I have some urls which I am looking to get the data depending upon the url called.  This is a school website, hence the namings ;)

 

Anyway, examples of the url are:

 

1. http://www.northfieldhouse.leicester.sch.uk/parents.php

2. http://www.northfieldhouse.leicester.sch.uk/parents.php?action=Letters%20Home

3. http://www.northfieldhouse.leicester.sch.uk/parents.php?action=Previous%20Letters&menu=Letters%20Home&subItem=Letters%20Home

 

Basically, what I'm looking to do, is get a regular expression that will see that there are no actions (i.e. there is no ?action=BlahBlahBlah as in 1.), will get the action (as in the case of 2.), and will get the menu and action (as in case 3).

 

I have no knowledge of regular expressions, and don't even know whether this is possible, but should imagine so.  But looking through the php manual site, I want something like the example below:

 

<?php

$str = 'foobar: 2008';

preg_match('/(?<name>\w+): (?<digit>\d+)/', $str, $matches);

print_r($matches);

?>

 

So I can call the array for the value of "action" and value of "menu" (in the code above, the names of the array values are name & digit).  If in case 1 (i.e. there is no action), then would this create an empty array?!

 

Sorry, I'm having a complete blonde day at the moment, and with little sleep and nutrition, I can't think properly (doh).

 

Many thanks in advace :)

 

David

Link to comment
Share on other sites

if (!preg_match('/action=[^&]+/', $str, $matches))
{
    // no action
}
else
{
    print_r($matches);
}

 

That will return something like "action=blah_blah". If you add in some brackets to the regex you can return the value of action as well, like this:

 

/action=([^&]+)/

Link to comment
Share on other sites

Perhaps something along the lines of?

 

$str = array('http://www.northfieldhouse.leicester.sch.uk/parents.php', 'http://www.northfieldhouse.leicester.sch.uk/parents.php?action=Letters%20Home', 'http://www.northfieldhouse.leicester.sch.uk/parents.php?action=Previous%20Letters&menu=Letters%20Home&subItem=Letters%20Home');

foreach($str as $val){
$parse = parse_url($val);
if(isset($parse['query']) && preg_match('#(action=[^&]+).+?(menu=[^&]+)?#', $parse['query'], $match)){
	echo $val . '<br />' . $match[1] . '<br />'; // $match[1] = action
	echo (isset($match[2]))? $match[2] . '<br /><br />' : '<br />'; // $match[2] = menu
} else {
	echo $val . "<br />" . 'No actions found!' . "<br /><br />\n"; // just like in example 1.
}
}

 

Output:

http://www.northfieldhouse.leicester.sch.uk/parents.php
No actions found!

http://www.northfieldhouse.leicester.sch.uk/parents.php?action=Letters%20Home
action=Letters%20Hom

http://www.northfieldhouse.leicester.sch.uk/parents.php?action=Previous%20Letters&menu=Letters%20Home&subItem=Letters%20Home
action=Previous%20Letters
menu=Letters%20Home

 

EDIT - I'm not sure if you already have your urls in an array or not.. I just used an array for a quick looping example...

Link to comment
Share on other sites

Do you really need regular expressions for this?  From what I gathered from the first post, the following should be what you are wanting: to get at the query string items in each url.

 

$urls = array(
'http://www.northfieldhouse.leicester.sch.uk/parents.php',
'http://www.northfieldhouse.leicester.sch.uk/parents.php?action=Letters%20Home',
'http://www.northfieldhouse.leicester.sch.uk/parents.php?action=Previous%20Letters&menu=Letters%20Home'
);

foreach ($urls as $url)
{
// Get query string portion of URL
$query_string = (string) parse_url($url, PHP_URL_QUERY);
parse_str($query_string, $query_array);

// Pad with default values
$query_array += array('action' => '', 'menu' => '');

echo '<h3>', $url, '</h3>';
echo '<pre>', print_r($query_array, TRUE), '</pre>';
}

Link to comment
Share on other sites

Do you really need regular expressions for this?

 

Like many other problems, regex is not always needed at all. You'll find people trying to parse html with regex while DOM / XPath will do for example.

As for this case, who knows what the OP is looking for exactly, as IMO it isn't made clear...

 

Yes, you'll find people asking for regex when regex might not be needed. Like anything else, there are many ways to skin a cat. And yes, regex is not always the answer, I agree. In most cases, the speed differences between regex and non-regex is probably negligible (depending on the task at hand and the scope of the data obviously... although admittedly I do prefer the faster route).

 

I'm sure a vast majority of problems could do without regex TBH.

 

EDIT - In your code, I don't think you need to type cast parse_url($url, PHP_URL_QUERY) as a string, being that the returned component PHP_URL_QUERY is already treated as such.

Link to comment
Share on other sites

EDIT - In your code, I don't think you need to type cast parse_url($url, PHP_URL_QUERY) as a string, being that the returned component PHP_URL_QUERY is already treated as such.

Whilst it makes no real difference at all, I'd rather pass a string to parse_str at all times.  If parse_url cannot find a query string, it returns NULL which is then cast to an empty string.  So, no requirement to cast to a string just a personal choice at the time.

 

Link to comment
Share on other sites

Thanks for all the help on this guys.  As stated, found a solution where regex wasn't neccessary, and appears to be along same line as what most people wrote ;)

 

For anyone interested, this is how I did it.  I create a function (since this is going to be called from many different pages), which looks like:

 

function createtitle($url, $deafultTitle) {
   if(is_array($url) && isset($url["query"])) {
  // Explode into key/value array
   $keyvalue_list=explode("&",($url["query"]));
   foreach($keyvalue_list as $key=>$value) {
    // Explode each individual key/value into an array
    $keyvalue=explode("=",$value);
// Make sure we have a "key=value" array
    if(count($keyvalue)==2) {
     // Create action and menu variables (as required)
     //echo "key=$keyvalue[0], value=$keyvalue[1]<br>";
     if (strtolower($keyvalue[0]) == "action") {
      $action = urldecode($keyvalue[1]);
     }
     elseif (strtolower($keyvalue[0]) == "menu") {
      $menu = urldecode($keyvalue[1]);
     }
    }
   }
  }

  if ($action != "") {
   if ($menu != "") {
    $title = $menu." - ".$action;
   }
   else {
    $title = $action;
   }
  }
  else {
   $title = $deafultTitle;
  }
  
  // Return the title
  return $title;
}

 

Then call this with the following:

 

$title = createtitle(parse_url($referrer), "Information for Parents"); 

 

Works perfectly how I need it :)

 

Thanks again guys

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.