Jump to content

Help with arrays


adambedford

Recommended Posts

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!

Link to comment
Share on other sites

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
)

 

Link to comment
Share on other sites

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)?

Link to comment
Share on other sites

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
)

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.