Jump to content

Extracting String from URL, Including Page in another Page Based on that String


michaelnorth

Recommended Posts

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:

 

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

 

<?php echo $keyword; ?>

 

Example: The URL http://www.domain.com/Santa-Cruz,-California-aardvarks-make-especially-good-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?

What your doing doesn't make a whole deal of sense to me. If you wanted to send a specific string to a specific page, the standard method would be using (real or fake) directories in the path. So for example if you wanted to get the string 'aardvarks make especially good tacos' on a page called 'santa-cruz-california.php', you would normally use a URL such as...

 

http://www.domain.com/santa-cruz-california/aardvarks-make-especially-good-tacos

 

This is easily forwarded using mod_rewrite.

the standard method would be using (real or fake) directories in the path

 

Well, I think that would work fine for my purposes. The URL could be of the form domain.com/first-string/second-string.php.

 

But perhaps I'm not explaining the whole idea clearly.

 

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 "Costa-Mesa, California" would include costa-mesa-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.

It just sounds like your overly complicating things. Let's imagine the user inputs the URL I used in my previous post...

 

http://www.domain.com/santa-cruz-california/aardvarks-make-especially-good-tacos

 

On your site you have the following in an .htaccess file.

 

Options +FollowSymLinks  
RewriteEngine On  

RewriteRule ^([^/]+)/([^/]+)/?$ /index.php?page=$1&keyword=$2

 

What the visitor will actually see is index.php. On that script you can then use $_GET['page'] and $_GET['keyword'] to find the values entered by the user.

Thanks, but if you're saying the user actually sees index.php as the filename, that's going the opposite direction of what I'm intending. I'm doing this for SEO reasons. Paid ads with specific embedded keywords drive traffic to the page. The page then displays using that keyword as its filename. Then the keywords are echoed on the page in various locations. And finally, the keyword includes a like-named file in the page. That included file contains custom articles, rotating images, perhaps some video, etc. The idea is to create a custom page on-the-fly for each of dozens of communities in the market area.

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.