Jump to content


Photo

how do i create a php extension - eg. if X is in the URL, then..


  • Please log in to reply
4 replies to this topic

#1 robys

robys
  • New Members
  • Pip
  • Newbie
  • 2 posts

Posted 18 August 2006 - 04:56 AM

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 '   ' ;

else
echo ' regular text goes here '

}


thanks, much appreciated!

#2 manmadareddy

manmadareddy
  • Members
  • PipPipPip
  • Advanced Member
  • 50 posts

Posted 18 August 2006 - 05:04 AM

Please use preg_match function
ex:
<?php
$subject = "abcdef";
$pattern = '/^def/';
preg_match($pattern, $subject, $matches, PREG_OFFSET_CAPTURE, 3);
print_r($matches);
?>
ex:
<?php
// get host name from URL
preg_match('@^(?:http://)?([^/]+)@i',
    "http://www.php.net/index.html", $matches);
$host = $matches[1];

// get last two segments of host name
preg_match('/[^.]+\.[^.]+$/', $host, $matches);
echo "domain name is: {$matches[0]}\n";
?>

#3 trq

trq
  • Staff Alumni
  • Advanced Member
  • 31,041 posts

Posted 18 August 2006 - 05:09 AM

Can you give us an example of what this url might look like? There may be a better way.

#4 Jeremysr

Jeremysr
  • Members
  • PipPipPip
  • Advanced Member
  • 199 posts
  • LocationSaskatchewan, Canada

Posted 18 August 2006 - 05:09 AM

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

$url = $_SERVER['PHP_SELF'];
if (strstr($url, "buy-")) {
     echo ("buy- is in the url");
} else {
     echo ("buy- isn't in the url");
}


#5 robys

robys
  • New Members
  • Pip
  • Newbie
  • 2 posts

Posted 18 August 2006 - 05:22 AM

The buy- URL is a mod-rewrite URL.

eg. www.domain.com/buy-product.html

The php page is eg. www.domain.com/product.php?id=XXXX

Unfortunately the code jeremy provided dindt work

thanks!




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users