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
https://forums.phpfreaks.com/topic/207150-help-with-arrays/
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
https://forums.phpfreaks.com/topic/207150-help-with-arrays/#findComment-1083122
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
https://forums.phpfreaks.com/topic/207150-help-with-arrays/#findComment-1083124
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
https://forums.phpfreaks.com/topic/207150-help-with-arrays/#findComment-1083126
Share on other sites

Archived

This topic is now archived and is closed to further replies.

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