lovelldr Posted June 8, 2009 Share Posted June 8, 2009 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 Quote Link to comment https://forums.phpfreaks.com/topic/161354-solved-regex-to-parse-url/ Share on other sites More sharing options...
lovelldr Posted June 8, 2009 Author Share Posted June 8, 2009 Could a mod delete the actual server name pls? As meant to edit out before posted but forgot, and can't seem to edit my post... Cheers Quote Link to comment https://forums.phpfreaks.com/topic/161354-solved-regex-to-parse-url/#findComment-851483 Share on other sites More sharing options...
Adam Posted June 8, 2009 Share Posted June 8, 2009 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=([^&]+)/ Quote Link to comment https://forums.phpfreaks.com/topic/161354-solved-regex-to-parse-url/#findComment-851519 Share on other sites More sharing options...
nrg_alpha Posted June 8, 2009 Share Posted June 8, 2009 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... Quote Link to comment https://forums.phpfreaks.com/topic/161354-solved-regex-to-parse-url/#findComment-851568 Share on other sites More sharing options...
salathe Posted June 9, 2009 Share Posted June 9, 2009 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>'; } Quote Link to comment https://forums.phpfreaks.com/topic/161354-solved-regex-to-parse-url/#findComment-851995 Share on other sites More sharing options...
nrg_alpha Posted June 9, 2009 Share Posted June 9, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/161354-solved-regex-to-parse-url/#findComment-852006 Share on other sites More sharing options...
salathe Posted June 9, 2009 Share Posted June 9, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/161354-solved-regex-to-parse-url/#findComment-852610 Share on other sites More sharing options...
nrg_alpha Posted June 9, 2009 Share Posted June 9, 2009 Ah, I see. Quote Link to comment https://forums.phpfreaks.com/topic/161354-solved-regex-to-parse-url/#findComment-852630 Share on other sites More sharing options...
lovelldr Posted June 11, 2009 Author Share Posted June 11, 2009 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 Quote Link to comment https://forums.phpfreaks.com/topic/161354-solved-regex-to-parse-url/#findComment-853743 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.