Jump to content

Archived

This topic is now archived and is closed to further replies.

robys

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

Recommended Posts

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!

Share this post


Link to post
Share on other sites
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";
?>

Share this post


Link to post
Share on other sites
Can you give us an example of what this url might look like? There may be a better way.

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites
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!

Share this post


Link to post
Share on other sites

×

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.