TalkPHP
 
 
Account Login
Latest Articles
» The basic usage of PHPTAL, a XML/XHTML template library for PHP
» Vulnerable methods and the areas they are commonly trusted in.
» Simple way to protect a form from bot
» The Basics On: How Session Stealing Works
» How to keep your forms from double posting data
IRC Channel
IRC Speech Bubble Join the friendly bunch on IRC...
(#TalkPHP on Freenode)

...Also available via a web interface.

See this thread for information on the TalkPHP Free Hugs Initiative™. Subject to availability.
Associates
Associates
CSS Tutorials
Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old 05-05-2008, 07:26 AM   #1 (permalink)
The Prestige
Upcoming Programmer Inquisitive 
 
Tanax's Avatar
 
Join Date: Sep 2007
Location: Sweden, Stockholm
Posts: 1,080
Thanks: 115
Tanax is on a distinguished road
Default Randomize code

Hi!

How could make a random coded string which has the markup like the one below:

Code:
XXXX-XXX-XXX-XX-XXXX
I understand that I have to use random, or some form of array splits.. but.. how?
__________________
Tanax is offline  
Reply With Quote
Old 05-05-2008, 07:48 AM   #2 (permalink)
Orc
The Prestige
 
Orc's Avatar
 
Join Date: Dec 2007
Posts: 1,044
Thanks: 193
Orc is on a distinguished road
Default

Quote:
Originally Posted by Tanax View Post
Hi!

How could make a random coded string which has the markup like the one below:

Code:
XXXX-XXX-XXX-XX-XXXX
I understand that I have to use random, or some form of array splits.. but.. how?
Here you go:
PHP Code:

<?php

$string 
"TEST-Aijnn-Bvvv";

$string explode('-',$string);

$string $string[0].'-'.$string[1].'-'.$string[2];

echo 
$string;
?>
change the string however you want.
__________________
VillageIdiot can have my babbies ;d

Last edited by Orc : 05-05-2008 at 08:24 AM.
Orc is offline  
Reply With Quote
The Following User Says Thank You to Orc For This Useful Post:
Tanax (05-05-2008)
Old 05-05-2008, 08:28 AM   #3 (permalink)
The Acquainted
 
sjaq's Avatar
 
Join Date: Nov 2007
Location: Netherlands
Posts: 113
Thanks: 11
sjaq is on a distinguished road
Default

Something like this?
PHP Code:
$str 'XXXX-XXX-XXX-XX-XXXX';
echo 
preg_replace_callback('/X/'create_function('$match''return chr(rand(65, 90));'), $str); 
sjaq is offline  
Reply With Quote
The Following 2 Users Say Thank You to sjaq For This Useful Post:
Tanax (05-05-2008), Yoosha (05-27-2008)
Old 05-05-2008, 08:37 AM   #4 (permalink)
The Prestige
Upcoming Programmer Inquisitive 
 
Tanax's Avatar
 
Join Date: Sep 2007
Location: Sweden, Stockholm
Posts: 1,080
Thanks: 115
Tanax is on a distinguished road
Default

Thanks!
Is it possible to also have it with numbers? :O
__________________
Tanax is offline  
Reply With Quote
Old 05-05-2008, 08:40 AM   #5 (permalink)
Orc
The Prestige
 
Orc's Avatar
 
Join Date: Dec 2007
Posts: 1,044
Thanks: 193
Orc is on a distinguished road
Default

Quote:
Originally Posted by Tanax View Post
Thanks!
Is it possible to also have it with numbers? :O
Sorry, my code wasnt complete enough, here:
PHP Code:
<?php

$string 
"TEST-Aijnn-Bvvv-66-bb-ff";

$string explode('-',$string);

$string soundex($string[0]).'-'.soundex($string[1]).'-'.soundex($string[2]);

// This encodes the pieces between each hyphen, course you can
// always do this with a for loop or something, and have what
// you want

echo $string;
?>
__________________
VillageIdiot can have my babbies ;d
Orc is offline  
Reply With Quote
The Following 2 Users Say Thank You to Orc For This Useful Post:
Tanax (05-05-2008), Yoosha (05-27-2008)
Old 05-05-2008, 08:52 AM   #6 (permalink)
The Acquainted
 
sjaq's Avatar
 
Join Date: Nov 2007
Location: Netherlands
Posts: 113
Thanks: 11
sjaq is on a distinguished road
Default

With numbers
PHP Code:
<?php 

    $str 
'XXXX-XXX-XXX-XX-XXXX';
    
    echo 
preg_replace_callback('/X/'create_function('$match''return rand(0, 1)? chr(rand(65, 90)) : chr(rand(48, 57));'), $str);

?>
@Orc: your code will generate the same string every time so it isn't that random
sjaq is offline  
Reply With Quote
The Following User Says Thank You to sjaq For This Useful Post:
Tanax (05-05-2008)
Old 05-05-2008, 08:53 AM   #7 (permalink)
Orc
The Prestige
 
Orc's Avatar
 
Join Date: Dec 2007
Posts: 1,044
Thanks: 193
Orc is on a distinguished road
Default

By the way, why is this in Absolute Beginners, I thought Tanax was more than a just a Beginner? :P
__________________
VillageIdiot can have my babbies ;d
Orc is offline  
Reply With Quote
Old 05-05-2008, 09:00 AM   #8 (permalink)
The Acquainted
 
sjaq's Avatar
 
Join Date: Nov 2007
Location: Netherlands
Posts: 113
Thanks: 11
sjaq is on a distinguished road
Default

@Orc I think this is pretty basic stuff so it belongs in the Beginners section
sjaq is offline  
Reply With Quote
Old 05-05-2008, 09:01 AM   #9 (permalink)
Orc
The Prestige
 
Orc's Avatar
 
Join Date: Dec 2007
Posts: 1,044
Thanks: 193
Orc is on a distinguished road
Default

Quote:
Originally Posted by sjaq View Post
@Orc I think this is pretty basic stuff so it belongs in the Beginners section
heh. what do you mean by basic?
__________________
VillageIdiot can have my babbies ;d
Orc is offline  
Reply With Quote
Old 05-05-2008, 09:06 AM   #10 (permalink)
Orc
The Prestige
 
Orc's Avatar
 
Join Date: Dec 2007
Posts: 1,044
Thanks: 193
Orc is on a distinguished road
Default

BY the way, heres another method
PHP Code:
<?php

$array 
= array('test','test','test','test','test','test');
$string implode('-',$array);


echo 
$string;



?>
__________________
VillageIdiot can have my babbies ;d
Orc is offline  
Reply With Quote
Old 05-05-2008, 09:06 AM   #11 (permalink)
The Prestige
Upcoming Programmer Inquisitive 
 
Tanax's Avatar
 
Join Date: Sep 2007
Location: Sweden, Stockholm
Posts: 1,080
Thanks: 115
Tanax is on a distinguished road
Default

Works like a charm! Thanks

@Orc- Well.. erh.. I'm farily good. But since this is really basic stuff, it belongs in the beginners section ;P
I mean, it's not really any "advanced" functions, it's just basic random and preg replaces..
__________________
Tanax is offline  
Reply With Quote
Old 05-05-2008, 09:08 AM   #12 (permalink)
Orc
The Prestige
 
Orc's Avatar
 
Join Date: Dec 2007
Posts: 1,044
Thanks: 193
Orc is on a distinguished road
Default

Quote:
Originally Posted by Tanax View Post
Works like a charm! Thanks

@Orc- Well.. erh.. I'm farily good. But since this is really basic stuff, it belongs in the beginners section ;P
I mean, it's not really any "advanced" functions, it's just basic random and preg replaces..
meh i was thinking about preg replacements, but i decided to try the simplest way possible, but oh well. good luck with whatever your doing
__________________
VillageIdiot can have my babbies ;d
Orc is offline  
Reply With Quote
Old 05-05-2008, 09:31 AM   #13 (permalink)
The Acquainted
 
sjaq's Avatar
 
Join Date: Nov 2007
Location: Netherlands
Posts: 113
Thanks: 11
sjaq is on a distinguished road
Default

Orc I think you don't get the thing he's trying here, he wants to have random strings not the same string every time you run the script.
sjaq is offline  
Reply With Quote
Old 05-05-2008, 09:33 AM   #14 (permalink)
Orc
The Prestige
 
Orc's Avatar
 
Join Date: Dec 2007
Posts: 1,044
Thanks: 193
Orc is on a distinguished road
Default

Quote:
Originally Posted by sjaq View Post
Orc I think you don't get the thing he's trying here, he wants to have random strings not the same string every time you run the script.
I know that, but Im too lazy to put in the code, as I said the code is incomplete, now shush.
__________________
VillageIdiot can have my babbies ;d
Orc is offline  
Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


All times are GMT. The time now is 11:32 PM.

 
     

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Inactive Reminders By Icora Web Design