michaelnorth Posted November 20, 2009 Share Posted November 20, 2009 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? Link to comment https://forums.phpfreaks.com/topic/182334-extracting-string-from-url-including-page-in-another-page-based-on-that-string/ Share on other sites More sharing options...
cags Posted November 21, 2009 Share Posted November 21, 2009 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. Link to comment https://forums.phpfreaks.com/topic/182334-extracting-string-from-url-including-page-in-another-page-based-on-that-string/#findComment-962647 Share on other sites More sharing options...
michaelnorth Posted November 22, 2009 Author Share Posted November 22, 2009 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. Link to comment https://forums.phpfreaks.com/topic/182334-extracting-string-from-url-including-page-in-another-page-based-on-that-string/#findComment-963041 Share on other sites More sharing options...
emopoops Posted November 22, 2009 Share Posted November 22, 2009 check if the url contains any of the "strings" you need to check for? then use explode() to get the other one or no im not sure Link to comment https://forums.phpfreaks.com/topic/182334-extracting-string-from-url-including-page-in-another-page-based-on-that-string/#findComment-963045 Share on other sites More sharing options...
michaelnorth Posted November 22, 2009 Author Share Posted November 22, 2009 Thanks, but I'm so new that I can't implement that myself. I'm looking for actual code that works if possible ;-) Link to comment https://forums.phpfreaks.com/topic/182334-extracting-string-from-url-including-page-in-another-page-based-on-that-string/#findComment-963047 Share on other sites More sharing options...
cags Posted November 22, 2009 Share Posted November 22, 2009 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. Link to comment https://forums.phpfreaks.com/topic/182334-extracting-string-from-url-including-page-in-another-page-based-on-that-string/#findComment-963103 Share on other sites More sharing options...
michaelnorth Posted November 22, 2009 Author Share Posted November 22, 2009 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. Link to comment https://forums.phpfreaks.com/topic/182334-extracting-string-from-url-including-page-in-another-page-based-on-that-string/#findComment-963362 Share on other sites More sharing options...
cags Posted November 22, 2009 Share Posted November 22, 2009 The user will see what they typed in the address bar, nothing else. Link to comment https://forums.phpfreaks.com/topic/182334-extracting-string-from-url-including-page-in-another-page-based-on-that-string/#findComment-963363 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.