robys Posted August 18, 2006 Share Posted August 18, 2006 Hello,How do I create a PHP if statement that does the following...If X is in the URL (eg. buy- ), then echo ..... if X isn't in the URL, then echo ...something like..if URL(buy-*) {echo ' ' ;elseecho ' regular text goes here '}thanks, much appreciated! Quote Link to comment Share on other sites More sharing options...
manmadareddy Posted August 18, 2006 Share Posted August 18, 2006 Please use preg_match functionex:<?php$subject = "abcdef";$pattern = '/^def/';preg_match($pattern, $subject, $matches, PREG_OFFSET_CAPTURE, 3);print_r($matches);?> ex:<?php// get host name from URLpreg_match('@^(?:http://)?([^/]+)@i', "http://www.php.net/index.html", $matches);$host = $matches[1];// get last two segments of host namepreg_match('/[^.]+\.[^.]+$/', $host, $matches);echo "domain name is: {$matches[0]}\n";?> Quote Link to comment Share on other sites More sharing options...
trq Posted August 18, 2006 Share Posted August 18, 2006 Can you give us an example of what this url might look like? There may be a better way. Quote Link to comment Share on other sites More sharing options...
Jeremysr Posted August 18, 2006 Share Posted August 18, 2006 You can get the current URL by using the variable $_SERVER['PHP_SELF']Then to see if "buy-" is in the URL, you use the strstr function. Which returns true if it finds a certain string inside the string. So...[code=php:0]$url = $_SERVER['PHP_SELF'];if (strstr($url, "buy-")) { echo ("buy- is in the url");} else { echo ("buy- isn't in the url");}[/code] Quote Link to comment Share on other sites More sharing options...
robys Posted August 18, 2006 Author Share Posted August 18, 2006 The buy- URL is a mod-rewrite URL. eg. www.domain.com/buy-product.htmlThe php page is eg. www.domain.com/product.php?id=XXXXUnfortunately the code jeremy provided dindt workthanks! Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.