Jump to content

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


robys

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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

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