Better PPV Target Trimming to Root Domain

Using DKI (Dynamic Keyword Insertion) is helpful to increase CTR on your landing pages.

Normally this is a fairly simple task. If you’re using Prosper202 and you’ve got your campaign and tracking setup correctly, 202 should pass your keyword in the URL to your landing page. So you’re landing page URL will look something like this:

To “grab” this keyword and use it in your page, you simply do something like this:

$target = $_GET['t202kw'];

<h1>Welcome <?php echo $target; ?> Visitors!!!</h1>

(For more about this, check out I don’t get GET’s!)

The Problem

Depending on what target your bidding on in PPV, many times these keywords aren’t “perfect” domains. If So you might end up with something like this:

Welcome ./ Visitors!!!

That’s lame.

This Function

…basically trims the target to its root domain and makes it as pretty as possible so you don’t look like a tool to your visitors. I’ve seen a few bits of code floating around that promise to do this for you, but they’ve all done a poor job (a.k.a. suck). Most of them don’t trim subdomains and many of them don’t handle pre-pended and trailing slashes. This one does.

Here’s an example of what this function can do: = = = =
/ =
./ = = = =
// = =
/ =
// =
// =

The Code

function trim_url_to_root_domain($target) {
/** Trim Url to Root Domain
* by 8/1/2011
* This function is mainly useful for PPV. You can pass it a URL (target) and it
* will trim it down to the root domain.  Unlike most other examples I've found,
* It handles subdomains and squirrely, or malformed URLs like "//"
* The result is a nice looking domain for dynamic keyword insertion into your
* landing pages.
* Usage:
* echo trim_url_to_root_domain("");
* or
* echo ucfirst (trim_url_to_root_domain("")); // uppercase first letter
    // trim http, https, and //
    $target = str_replace("http:", "", $target);
    $target = str_replace("https:", "", $target);
    $target = str_replace("//", "", $target);

    // check for dots
    $dots = substr_count($target, '.');
    if ($dots > 1) { // if there are more than 1 dots, we need to remove subdomain
        $first_dot = strpos ($target, ".");
        $target = substr($target, $first_dot+1, strlen($target));

    // find the last slash, this should be the slash just before directory info
    $last_slash = strripos($target, "/");
    if ($last_slash > 0 ) {
        $target = substr($target, 0, $last_slash);

    // find last slash one more time, to handle targets like /
    $last_slash = strripos($target, "/");
    if ($last_slash !== FALSE ) {
        $target = substr($target, 1, strlen($target));

    // normalize target so it's all lower case
    $target = strtolower($target);

    return $target;


I hope most of you know how to use this, but just in case…. Copy & paste this function at the bottom of your LP. Your LP should have a .php extension. To grab the keyword from 202, use the example I gave above. Then, instead of this:

<h1>Welcome <?php echo $target; ?> Visitors!!!</h1>

do this:

<h1>Welcome <?php echo trim_url_to_root_domain($target); ?> Visitors!!!</h1>

That’s it! You can also capitalize the first letter of the domain using PHP’s ucfirst function. Check out the comments in the function for how to do this.

You Break it You Buy It

So far this function has worked pretty well for me, but nobody’s perfect.  If you happen to find a target that doesn’t get trimmed correctly, please post in the comments below and I’ll see what I can do to fix it.

But Wait, There’s More!

I did a guest post on PPV Playbook Blog today called “Killer Buttons To Squeeze More CTR From Your Landing Pages“.  Check it out!

Leave a comment



CommentLuv badge


  • Thanks for the share! I’ve thought of implementing a few of these on my broader targets to test but have been too lazy to go grab the code.

    [side note] The lame affbuzz bar things blocks the capability for me to comment on your blog unless I break their frame 🙂

    • Glad you like the code. Bummer about that affbuzz thing. I’ll have to figure out a workaround. Thanks for the heads up.

  • Great script! Thanks for sharing 🙂

    • You’re welcome, thanks for reading!

  • windir

    This script will only trim correctly if the $target is containing one subdomain section ( ex: would end up with )

    This can be prevented by putting the “check for dots” section in a loop. Removing one dot at the time until only one dot is left.

    That shouldn’t be a big problem tho since there aint a lot of deeper level subdomains than one level normally, but never say never 😉

    • You’re right, it won’t handle 2 subdomains. So far I’ve never used any targets like that, but I guess it’s worth noting. Thanks for reading and commenting!

  • Good stuff. You can also use regex:

    Welcome Visitors!!!

    • Hmm.. I think you may have been trying to include some regex code and WordPress filtered it out. Sorry about that!

      You can indeed use regex for this. I tried some off the shelf regex commands that other people posted but none of them handled all of the target variations I was using so I rolled my own. I suck at regex myself, so I rarely use it.

      Thanks for reading!

  • Glad you wrote again. I don’t understand half of that code so I’ll try and copy/paste 🙂
    browie’s latest blog post: Over a Month of Work

    • Nothing wrong with copying & pasting bro! Thanks for reading.

  • Compound

    ctrtard – Thanks once again for another awesome script! One of the guys over at PPVP pointed me to this post.

    • You’re welcome man. Thanks for the feedback!

  • Aysha

    I don’t know why its not working for me. Most of the ppv network don’t accept http://www.

    So I removed that command and I’m getting the result for like this:

    the first letter missing and the last level of the url

    • There is no need to remove anything. The last directory level is getting chopped off because the script is not expecting more than one slash after the domain. The reason is it’s often silly to bid on targets like that in ppv. Anyone bidding on the domain itself will grab most or all of the traffic.

    • pata33

      Thanks so much for this script. I have tried many like it over the last few days and this one is the best!

      Like Aysha, I too have a specific strategy that requires me to bid on very specific long-tail domains that contain many directories and therefore slashes.

      I was able to make a very simple modification to the existing script.

      Find the below code in the existing script…
      // find the last slash, this should be the slash just before directory info
      $last_slash = strripos($target, “/”);
      if ($last_slash > 0 ) {
      $target = substr($target, 0, $last_slash);


      Replace it with…..

      // find the first slash, this should be the slash just before directory info
      $first_slash = strripos($target, “/”);
      if ($first_slash > 0 ) {
      $target = substr($target, 0, $first_slash);

      // find the second slash
      $second_slash = strripos($target, “/”);
      if ($second_slash > 0 ) {
      $target = substr($target, 0, $second_slash);

      You can repeat this as many times as you like…
      ie. $third_slash, $fourth_slash……

      Seems to work for me. Hope it helps.

  • Alex

    ctrtard please help me with this script – I put the code in separate file( But there is an error “Parse error: syntax error, unexpected T_STRING in /home8/elearnl1/public_html/filloutthesurvey/dc/trim.php on line 23”. I also tried to use this function on landing page – and got the same result.

    • Hi Alex,

      I’m betting you introduced an error when you copy & pasted the script. When you hover your mouse over the code block above, you will see some buttons appear in the top right corner. One of them says “View source”. Click on that and a new window will open. Copy & paste that code into your trim.php file and see if that fixes it.

      • Alex

        You was right. Thanks a lot for help.

  • Max

    Hi ctrtard,

    Thanks for the wonderful lesson here. Just a quick question though – Which part of my lander do I insert the main code?

  • Robin

    Hi, really nice script!

    Could You help me to add additional code for trimming AdOn traffic sources, like, domain.com_113488

    Thanks in advance!

    • Robin

      // find _ like domain.com_824589
      $last_line = strripos($target, “_”);
      if ($last_line > 0) {
      $target = substr($target, 0, $last_line);

  • Jon

    Hi man,
    need your help! for some reason when I am copying this code into the lp it is simply printing out the entire code from 1) { // if there are more than 1 dots, we need to remove subdomain
    $first_dot = strpos…. onwards onto the landing page straight, i am guessing its a verrrry rookie mistake.


    • Does the page you’re pasting into have a .php extension?

      Only pages with a .php are parsed for php code.

  • Jon

    forgot to add i am putting in the code between and

  • Jon

    Hi, Yea it has a php extension as well.

    • Then you have a typo somewhere or you’ve corrupted the code when you pasted. Hover your mouse over the upper right section of the code block. Then click on the view source icon. Copy & Paste the source code you see.

      Know this: the code works fine. So whatever the problem is, it’s probably something small like a bad paste job.

  • Jon

    aaahh u were right it was just a typo in the code i was pasting. thanks for pointing that out :))

  • Cliff

    Could you explain the capitalize the first letter in the domain a little bit more? I had it displaying my target just fine but went to try and have it capitalize the first letter in the domain and things messed up from there and I can no longer even get the keyword to show up. No parse errors, it’s just not pulling the target/keyword through now.

    Any ideas what might cause that? I’ve tried to redo the copy/paste via your buttons. With my own code i have it displaying the keyword/target fine, just can’t get the capitalization part to work.

    • Assuming you are using this code to display your target (and it’s working OK)…

      echo trim_url_to_root_domain($target);

      You can use this code to capitalize the first letter.

      echo ucfirst (trim_url_to_root_domain($target));

  • Jon

    Ok so this is working great now! I did see however that bidding on domains trims down the display url to just, is there any way around this? thanks

  • qasy

    why don’t you use ‘explode’ function?

    for the subdomain issues(and UK ones) need only 2 lines more

  • qasy

    cutted ot the example:
    domain = explode(“/”,$kw);
    echo $domain[0];

    • You can definitely use explode. But that just gives you an array. You won’t know which array element is which. That’s the problem with handling these types of targets. There are a lot of variations.

      If you make a mod that you think improves on what I have here, feel free to shoot me an email and I’ll post the update. Thanks for reading!

  • mike

    This is not working for me, nothing is showing at all. The basic (non-shortening) version works fine.

    I copied the code and the function, so I know they are correct. Files is .php.


    • I am getting the same problem, I can’t figure it out at all, defo not a copy and paste problem either…

      • Hmm… it’s actually pretty simple to use. If you use pastebin and show me the exact code you’re using I might be able to help.

  • Works like a charm. Thanks for this awesome script


    Adi Rian
    Adi Rian’s latest blog post: Easy Way to Create Landing Page Using Photoshop and Dreamweaver

  • gorkem

    thanks for this post. it is great..

    BUT, It does not handle;

    if there is a slash after sub-directory, it does not work. I am working on it now, will fix it soon. It would be great if take a look at it

  • gorkem

    instead of using the code for last slash, I did this;

    $first_slash = stripos($target, “/”);
    $target = substr($target, 0, $first_slash);

    it worked great even if there is 5 sub directory.

    Thanks again for this codes, it is a huge time saver.

    • Thanks for sharing that fix!

  • Martin

    Does this work with CPVLab also or does it only work with prosper202?


    • Trimming script works with either. Example usage is designed for 202.

  • jason

    I have tried the script in 1 of my landing pages. It does not display anything. I placed the php function part in all possible locations (top of page, bottom etc etc) … no chance

    I tried another script (but which was not displaying just the domain) and that worked perfectly.

    What is possible to be wrong? Thank you for help

    • The script works fine. Not sure what you’re doing wrong without seen your source.

  • jason

    I just copied paste your php code. what I can not see (understand) in your code is GET function. How your code gets the keyword from the link?

    I use currently a script that is working well, but if there is an alias before the domain, such as, does not strip “something”, so displays

  • MoneyCometh


    Really appreciate your time and share. I tried the code and made the fix for “last dash” by gorkem but I don’t know if this is correct the way I have applied it. Here is a link to the code:

    I also don’t know how to intergrate this with CPVLAB? What do I need to put in CPVLAB and where?

  • John

    I’m nearly 100% sure the reason it’s not working for you guys is because you didn’t add this first section of code here:

    It’s at the top of the article so you probably glossed over it because it’s not 100% clear that you needed it. Without those three lines the function doesn’t grab the keyowrd and it doesn’t work.

    • John

      Uggh for some reason I can’t paste PHP code into a comment. Anyways it’s the 3 lines of code mentioned at the very top of the article.

  • luqman


    I still cannot figure it how to use with cpv lab. Can someone here help me?


    • I am using CPVlab too,Its not working for me 🙁

  • I just figured it out,sorry for the last comment…
    I added the top 3 line code on my landing page,now its working.but here is another problem:
    Like this link

    And the result is Welcome visitors of!
    And I tested other urls,the first letter is missing,anyone help?

  • Hi ctrtard, so far this script work great for me.
    Btw, how to make / =
    C uppercase?


    • Instead of:

      echo $target;


      echo ucfirst($target);

      See “ucfirst” in the PHP Manual for more info.


[…] Learn how to trim targets to the root domain here […]

Better PPV Target Trimming to Root Domain [Link] « August 6, 2014

[…] Learn how to trim targets to the root domain here […]

Better PPV Target Trimming to Root Domain [Link] « September 16, 2014


Related posts by category

Related posts by tag