PDA

View Full Version : Resolved G-Mail Dot Trick Generator

Deacon Frost
03-01-2009, 03:47 AM
It seems like a really simple concept, but I'm still baffled. I've actually been working on this little snippet for about 2 months now >.<.

What I'm trying to do:

Ok, you have a string "Cat". Now, you want to put periods in every possible combination in that string, except for before, and after the string. I NEED IT TO PRINT ALL POSSIBLE OUTCOMES, NOT JUST THE FINAL ^.^. So the possible outcomes are:

Cat
C.at
Ca.t
C.a.t

That math of it: !2^(n-1) == 2^(3-1) == 2^2 == 4

There's several ideas I have for going about this. Such as removing the first and last character, and adding white spaces in the entire string, then going about some kind of for loop (which I have no clue how it would work)...

Alright, Here's my plan of attack:

<?
// string string for dot placement
\$string = "cat";
\$length = strlen(\$string);
\$str1 = str_replace(\$string['0'],"");
\$str2 = str_replace(\$string[\$length],"");

\$newstring = \$str1 . "" . \$str2;

\$newstrlen = strlen(\$newstring);

for(\$i=0;\$i<=\$newstrlen;\$i++){

\$array = array("",".");
// to carry i

}

// and then.. the for loop

// determine number of for loops and assign value

for(\$l=0;\$l<=\$i;\$l++){

// Duh, doesn't work, but the concept is that this
// for loop is controlled from the other for loop, and
// goes through the string once, then the other for
// loop goes through it the other time

for(\$l=\$l;\$l <= \$i;\$l++){

// if it's a period, skip it
if(\$newstring[\$l] = "."){
\$l = \$l++;
}

// if the letter is a number, or a letter, skip it
elseif(ctype_alnum(\$newstring[\$l] !== false){
\$l = \$l++;
}

// if it's a white space, change to a period
else{
\$newstring[\$l] = ".";
}

// end fors

}

}

?>

As you can see, that will, in no way, work at all :P.

The thought behind it though is that if I take the first character, and the last character off, and then work with that string, it'll be better. The rest of it is the confusing part, the first part of the script works.

So, if you have any ideas on how to go about this, the algorithm, or anything like that, it'd be appreciated.

What is the G-Mail Dot Trick?

G-Mail uses a catch-all for emails like: "ca.t@gmail.com", it will send to "cat@gmail.com". The purpose for this ranges from several things, as it allows for multiple registrations on some sites, but all the emails come to the same address. It's rather effective. There's other methods as well, and I know that, but I need THIS method to work.

Deacon Frost
03-01-2009, 05:27 PM
Another method I'm attempting to use, and it looks like it might be on the right path:

<?php
// however if the string has 2 of the same characters, it messes up
\$string = "howdy";

// prevent a period at the very end of the string
\$strlen = strlen(\$string) - 2;

// print the base string
echo \$string . "<br />";

// loop while i <= strlen, right before last character
for(\$i=0;\$i<=\$strlen;\$i++){
\$str = \$string[\$i];

// put in the periods once after every \$i
\$newstr = chunk_split(\$str,1,".");

// try and do the first and last part of the string
\$arr = explode(\$string[\$i], \$string, 2);

// put it all together
echo \$arr[0] . "" . \$newstr . "" . \$arr[1] . "<br />";

}

?>

It works exactly like it should, but I don't know where to go from there, right now, I get:

howdy
h.owdy
ho.wdy
how.dy
howd.y

And I need now:

howdy
h.o.wdy
h.ow.dy
h.owd.y

And so on.

Also, if there's more than one character, for instance "hwowdy", it outputs this:

hwowdy
h.wowdy
hw.owdy
hwo.wdy
hw.owdy
hwowd.y

As you can see, instead of putting it after the last 'w', it puts it after the first one. Dunno how to fix that too. =/.

Any help is appreciated :D!

EDIT: I'm trying to use implode to see if that works, but still no progress >.< // Implode didn't work :p.

schleppel
03-01-2009, 08:04 PM
I'm not sure if this is the best way, but you can do it with binary.

0000
howdy

0001
howd.y

...

1110
h.o.w.dy

1111
h.o.w.d.y

<?php

\$string = "howdy";

\$string_length = strlen(\$string);
\$combinations = pow(2, (\$string_length - 1));

// Loop through the number of combinations.
for(\$i = 0; \$i < \$combinations; \$i++)
{
// Use the current combination number to get the binary for the . positions.
echo \$bin . "\n";
// Loop through the binary number.
for(\$j = 0; \$j < (\$string_length - 1); \$j++)
{
// Output the current letter.
echo \$string[\$j];
// Output a . if the current binary column is positive.
if(\$bin[\$j] == 1)
{
echo '.';
}
}
// Output the last letter.
echo \$string[\$j] . "\n\n";
}

Deacon Frost
03-01-2009, 09:26 PM
Seriously, thank you so freaking much.

I can only give one thanks, but if there was a way, I'd give so much more.

It works perfectly, and it doesn't really matter if it's not the normal way to approach it, as long as the outcome is accurate :).

Thanks.

hastinbe
03-01-2009, 09:32 PM
<?php
\$string = 'howdy';
\$separator = '.';

echo \$string . '<br/>';

\$strarray = str_split(\$string);
\$strarray[0] .= \$separator;

for (\$i = 1; \$i < count(\$strarray); \$i++) {
\$newarray = \$strarray;
\$newarray[\$i-1] .= (\$i > 1 ? \$separator : null);

echo implode('', \$newarray) . '<br/>';
}

I responded to you on #php, but you were afk.

- haro

oesxyl
03-01-2009, 11:58 PM
<?php
if(strlen(\$str) > 1){
\$ca = preg_split("//",\$str);
array_shift(\$ca);
array_pop(\$ca);
\$result = array();
foreach(\$res as \$val){
\$result[] = \$head . '.' .\$val;
}
return \$result;
}
return array(\$str);
}

print_r(\$res);
?>

best regards

Deacon Frost
03-02-2009, 12:47 AM
Thanks oesxyl :D! Maybe I can find a way to combine the two :p.

Anyone wanting to see my end result :

http://ispikedthepunch.com/Deacons%20PriVate/dotrick.php

abduraooft
03-02-2009, 06:55 AM
What is the G-Mail Dot Trick? I think you are not aware about the validity of email formats like

D.....................eacon..............Frost@gmail.com
You can have any(?) number of dots in between the characters.

Deacon Frost
03-02-2009, 05:05 PM
I think you are not aware about the validity of email formats like

D.....................eacon..............Frost@gmail.com
You can have any(?) number of dots in between the characters.

Yeah, but think about it. If you were to write a script to allow only one submission per person, you'd allow one dot, but multiple dots would be picked up by any experienced coder :P.. Yeah, you can have multiple dots, you can also have example+<anynumber>@gmail.com, and it'll work. But if the scripts pick up special characters, it might not work. This method is nearly fool proof :P.

oesxyl
03-02-2009, 10:50 PM
Yeah, but think about it. If you were to write a script to allow only one submission per person, you'd allow one dot, but multiple dots would be picked up by any experienced coder :P.. Yeah, you can have multiple dots, you can also have example+<anynumber>@gmail.com, and it'll work. But if the scripts pick up special characters, it might not work. This method is nearly fool proof :P.
your problem is interesting but in my opinion have nothing to do with mail addreses.
as far the address is valid acording to rfc's and exist somewhere on a server I don't see any reason to invent new rules.

best regards