adambedford Posted July 8, 2010 Share Posted July 8, 2010 I'm working on a website redesign and I'm under strict instruction to keep the URLs the same (they are static HTML pages) so I've figured out how to use mod_rewrite to redirect to my PHP script to decide what pages to include. My questions/problem is deciphering the original URL and extracting the info in order to make the right query/page include. The website is a villa rental site and the pages are formatted like this: costa-blanca-3-bedrooms.html. 1 problem is that the file names vary from costa-blanca-3-bedrooms.html to costa-del-sol-3-bedrooms.html to barcelona-3-bedrooms.html. As you can see, the region name differs in the number of words and therefore I can't use a strict template to extract the details into an array. My thoughts are that I could work backwards to automatically remove the last element of the array (which would be something like "bedrooms.html" as that info is useless. Then i could split my text/digits or work my way backwards, always knowing that the next element is going to be the number of bedrooms and what remains is going to be the region. How would I go about manipulating the array like this? Or does anyone have a potentially better solution? Any help would be really appreciated! Quote Link to comment Share on other sites More sharing options...
Wolphie Posted July 8, 2010 Share Posted July 8, 2010 I guess it really depends on how well formatted you want the array. For example: <?php $str = 'costa-del-sol-3-bedroom.html'; $details = explode('-', basename($str, '.html')); print_r($details); ?> The result would be: Array ( [0] => costa [1] => del [2] => sol [3] => 3 [4] => bedroom ) Quote Link to comment Share on other sites More sharing options...
adambedford Posted July 8, 2010 Author Share Posted July 8, 2010 Thanks for your reply! What happens when the region is only 2 words, i.e. costa-blanca-3-bedrooms.html? Because then $details[2] would go from being the last word of the region to the number of bedrooms. Can I somehow discard the last element and extract a number from the array? Therefore leaving the region (with the varying number of words/elements)? Quote Link to comment Share on other sites More sharing options...
wildteen88 Posted July 8, 2010 Share Posted July 8, 2010 Your can work backwards with regex. Here is an example mod_rewrite rule which may work for you RewriteRule ([a-z\-]+)-([0-9])-([a-z]+).html$ test.php?region=$1&$3=$2 To test create a file called test.php and have the following in it <?php echo '<pre>'.print_r($_GET, true).'</pre>'; ?> Now go to yoursite.com/costa-del-sol-3-bedrooms.html or barcelona-3-bedrooms.html You should get output similar to this Array ( [region] => costa-del-sol [bedrooms] => 3 ) Quote Link to comment Share on other sites More sharing options...
adambedford Posted July 8, 2010 Author Share Posted July 8, 2010 wildteen88: thank you for your suggestion! that looks like it will do the trick nicely! I'll test it out and hopefully I'll be able to report that all is working nicely. 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.