Regex Google Publisher Code

Discussion in 'PHP' started by gerard0986, Jan 11, 2010.

  1. #1
    What would the regex be in PHP to make sure a phrase is a Google Publisher code and not anything else?

    Like...
    "pub-1234567890123456"
     
    gerard0986, Jan 11, 2010 IP
  2. MyVodaFone

    MyVodaFone Well-Known Member

    Messages:
    1,048
    Likes Received:
    42
    Best Answers:
    10
    Trophy Points:
    195
    #2
    Why would it be anything else ? can you explain further what your trying to achieve.
     
    MyVodaFone, Jan 11, 2010 IP
  3. gerard0986

    gerard0986 Peon

    Messages:
    53
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    I want the Regex to make sure the inputted text is a Google Publisher Code and not some random numbers or letters or something.
     
    gerard0986, Jan 11, 2010 IP
  4. MyVodaFone

    MyVodaFone Well-Known Member

    Messages:
    1,048
    Likes Received:
    42
    Best Answers:
    10
    Trophy Points:
    195
    #4
    There is no know way to check if a publishers ID is valid or not, so I suggest you use a numbers only (16) validation check on your input, thus use the text pub- within your scripts, as above though, this could still be a random 16digit number, theres no way to check that its a valid publishers id, only to manually try it.
     
    MyVodaFone, Jan 11, 2010 IP
  5. astkboy2008

    astkboy2008 Peon

    Messages:
    211
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    0
    #5
    its very easy but i cant do it
    because iam bad in Regex
     
    astkboy2008, Jan 11, 2010 IP
  6. danx10

    danx10 Peon

    Messages:
    1,179
    Likes Received:
    44
    Best Answers:
    2
    Trophy Points:
    0
    #6
    Here you go, I created alittle function for you:

    The regex: #^pub-[0-9]{16}$#

    <?php
    
    function google_pub($pub)
    {
    $validate = preg_replace('#^pub-[0-9]{16}$#', "", $pub);
    
    if(empty($validate))
    	{
    	return true;
    	}
    	else
    	{
    	return false;
    	}
    }
    
    //1 = valid, 0 = not valid
    echo(google_pub("pub-1234567890123455"));
    
    ?>
    PHP:
     
    danx10, Jan 11, 2010 IP