Extracting String from URL, Including File on Page

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
michaelnorth
Forum Newbie
Posts: 4
Joined: Fri Nov 20, 2009 2:29 pm

Extracting String from URL, Including File on Page

Post by michaelnorth »

I'm new to PHP, bear with me. This is a two-part question.

I have a bit of code that extracts a string (I'm calling it a keyword) from a URL; it works fine:

Code: Select all

<?php
$keyword = "Aardvarks and Other Curious Animals"; //This term is assigned as the keyword if no keyword is provided
if ($_GET['q']) {
    $keyword = (preg_replace("/\-/", " ", strip_tags(trim($_GET['q'])))); // strips white space, tags, converts to sentence case
   // $keyword = preg_replace("/ Strip This Phrase/", "", $keyword); // This phrase is stripped out of the keyword
}
?>
So if I want to drop the keyword into a Web page, I'll just do this:

Code: Select all

<?php echo $keyword; ?>
Example: The URL http://www.domain.com/Santa-Cruz,-Calif ... -tacos.php echos back "Santa Cruz, California aardvarks make especially good tacos"

But I ALSO need to parse out just the first part of the URL string -- "Santa-Cruz,-California" to retrieve an existing file named "santa-cruz-california.php". This scenario will be repeated for a set of geographic names.

1. How do I extract just the first part of the URL? (we're probably talking about just one state if that helps)
2. How do I then build some code to include that like-named file on the page?
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Extracting String from URL, Including File on Page

Post by requinix »

How do you know what "the first part" is? Three words? Everything up to and including the first word after the first comma? What about places like New Hampshire?
michaelnorth
Forum Newbie
Posts: 4
Joined: Fri Nov 20, 2009 2:29 pm

Re: Extracting String from URL, Including File on Page

Post by michaelnorth »

tasairis wrote:How do you know what "the first part" is? Three words? Everything up to and including the first word after the first comma? What about places like New Hampshire?
That would have to be specified in the code I assume. In this case, the "first part" is everything after domain.com/ up to and including "California". In a more general case, isn't it possible to dump all 50 state names into an array that would cover all possibilities? When the script encounters one of the state names, it would end the extraction process.
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Extracting String from URL, Including File on Page

Post by requinix »

michaelnorth wrote:In a more general case, isn't it possible to dump all 50 state names into an array that would cover all possibilities? When the script encounters one of the state names, it would end the extraction process.
Sure, but it's not a nice solution.

Can you change the URL scheme? To something like

Code: Select all

http://www.example.com/California/Santa-Cruz/aardvarks-make-especially-good-tacos
Not only is it better for SEO but it's easier to work with in code.


If not then, with no other indication of where the location ends and the title begins, you'll have to parse the string for every possible location. Luckily you have City,-State, and there are only 50 states you have to look at, so you can look for the State and ignore the rest.
Just hope that you don't have to deal with locations outside of the US.
michaelnorth
Forum Newbie
Posts: 4
Joined: Fri Nov 20, 2009 2:29 pm

Re: Extracting String from URL, Including File on Page

Post by michaelnorth »

I don't think I can change the structure of the URL because it must make sense in an English sentence once it's parsed out (unless there's a way to rearrange the terms).

"Santa Cruz, California aardvarks make especially good tacos" makes sense. But the alternative does not:

California Santa Cruz aardvarks make especially good tacos
michaelnorth
Forum Newbie
Posts: 4
Joined: Fri Nov 20, 2009 2:29 pm

Re: Extracting String from URL, Including File on Page

Post by michaelnorth »

This would work though: domain.com/Santa-Cruz,-California/aardvarks-make-especially-good-tacos.php

that is: domain.com/first-string/second-string.php.

First step: Let's say domain.com/first-string/second-string.php points to the page that is build-on-the fly. And that some code exists on that page to extract the first-string and the second-string from the URL. Those strings could be then echoed at various places on the page as $string1 and $string2.

Second step: $string1 is used in an include statement to bring a separate file called first-string.php into that page. For example, say that $string1 is "Santa-Cruz, California" would include santa-cruz-california.php.

In a nutshell, that's all I'm trying to do. If it would help, I can post a working link to what I have so far (up to the point where I got stuck). Please advise.
Post Reply