Can anyone help w/ simple regular expression?

Sonny Forelli

Well-known member
May 8, 2008
2,330
89
48
Liberty City
I cobbled the following together to echo in the target in a ppv campaign.


$t202kw=$_GET["t202kw"];

$sitename = substr($t202kw, 0, stripos("$t202kw", "/"));



if ($sitename) echo $sitename; else echo 'Mydomain.com'; ?>

The goal being that if the t202kw=Amazon.com/blah/blahblah1890819.php it will still track this deep linked keyword but to the consumer will present Amazon.com

This works. However due to the nasty stripos thing it relies on the presence of the slash to work, if just a root domain target like Amazon.com is used sans slash it defaults to Mydomain.com

I know I should be using a regular expression, can anyone help me with one please? The goal being if domain.com to get this, and if domain.com/boah/falksdjf.html to get domain.com only and if BLANK to echo 'Mydomain.com"

Thank you
 


Code:
preg_match('/^([^\/]+)/', $_GET['t202kw'], $matches);
// matches start of string with ^ , then (one or more of any character that is not forward slash)
$sitename = $matches[0];
echo  "Sitename: $sitename";

crap, sorry, didn't read... this will return Amazon.com, if &t202kw=Amazon.com (no trailing slash).
If you want to default it to mydomain.com if there's no slash, you don't need regex at all --
Code:
$slash = strpos($_GET['t202kw'], '/');
$sitename = $slash === false ? 'MyDomain.com' : substr($_GET['t202kw'], 0, $slash);
 
kblessing's right, neither of my solutions are what you asked for


I used yours just like this:

preg_match('/^([^\/]+)/', $_GET['t202kw'], $matches);
// matches start of string with ^ , then (one or more of any character that is not forward slash)
$sitename = $matches[0];
if ($sitename) echo $sitename; else echo 'Mysitename.com';


works great- no longer defaults to Mysitename when there is no slash present.

:dunno:

kblessing - thank you, will play around with that as well just to understand.
 
I used yours just like this:

preg_match('/^([^\/]+)/', $_GET['t202kw'], $matches);
// matches start of string with ^ , then (one or more of any character that is not forward slash)
$sitename = $matches[0];
if ($sitename) echo $sitename; else echo 'Mysitename.com';


works great- no longer defaults to Mysitename when there is no slash present.

:dunno:

kblessing - thank you, will play around with that as well just to understand.

Typically you want to avoid regex when you can, it takes a bit more collective cpu power (ie: slower) especially when you have high traffic to do a preg expression on something so simple versus a simple explode and array.
 
  • Like
Reactions: Sonny Forelli
Code:
preg_match('/^([^\/]+)/', $_GET['t202kw'], $matches);
// matches start of string with ^ , then (one or more of any character that is not forward slash)
$sitename = $matches[0];
echo  "Sitename: $sitename";
crap, sorry, didn't read... this will return Amazon.com, if &t202kw=Amazon.com (no trailing slash).
If you want to default it to mydomain.com if there's no slash, you don't need regex at all --
Code:
$slash = strpos($_GET['t202kw'], '/');
$sitename = $slash === false ? 'MyDomain.com' : substr($_GET['t202kw'], 0, $slash);


see my post above- I want it to return amazon.com - your first example worked fine- just added the else to it. I want it to default to mydomain.com only if the keyword is blank, thus handling either root domain targets or deep linked targets and presenting the site name correctly to the visitor which yours does.

thanks again
 
If the domain name is always gona be like that...

just tried your code and it's returning blank

I have it just like this

Code:
<?php 
               $exploded = explode('/', $_GET['t202kw']);
$sitename = ($exploded[0] == '')?'Mydomain.com':$exploded[0]; 
               
                             
            ?>

blank with either ...t202kw=Targetdomain.com or targetdomain.com/