 |
Account Login
|
 |
 |
Latest Articles
|
 |
 |
IRC Channel
|
 |
 |
Associates
|
 |
 |
Associates
|
 |
|
 |
 |
|
 |
05-05-2008, 07:26 AM
|
#1 (permalink)
|
|
The Prestige
Join Date: Sep 2007
Location: Sweden, Stockholm
Posts: 1,080
Thanks: 115
|
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? 
__________________
|
|
|
|
05-05-2008, 07:48 AM
|
#2 (permalink)
|
|
The Prestige
Join Date: Dec 2007
Posts: 1,044
Thanks: 193
|
Quote:
Originally Posted by Tanax
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.
|
|
|
|
|
The Following User Says Thank You to Orc For This Useful Post:
|
|
05-05-2008, 08:28 AM
|
#3 (permalink)
|
|
The Acquainted
Join Date: Nov 2007
Location: Netherlands
Posts: 113
Thanks: 11
|
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);
|
|
|
|
|
The Following 2 Users Say Thank You to sjaq For This Useful Post:
|
|
05-05-2008, 08:37 AM
|
#4 (permalink)
|
|
The Prestige
Join Date: Sep 2007
Location: Sweden, Stockholm
Posts: 1,080
Thanks: 115
|
Thanks!
Is it possible to also have it with numbers? :O 
__________________
|
|
|
|
05-05-2008, 08:40 AM
|
#5 (permalink)
|
|
The Prestige
Join Date: Dec 2007
Posts: 1,044
Thanks: 193
|
Quote:
Originally Posted by Tanax
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
|
|
|
|
|
The Following 2 Users Say Thank You to Orc For This Useful Post:
|
|
05-05-2008, 08:52 AM
|
#6 (permalink)
|
|
The Acquainted
Join Date: Nov 2007
Location: Netherlands
Posts: 113
Thanks: 11
|
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
|
|
|
|
|
The Following User Says Thank You to sjaq For This Useful Post:
|
|
05-05-2008, 08:53 AM
|
#7 (permalink)
|
|
The Prestige
Join Date: Dec 2007
Posts: 1,044
Thanks: 193
|
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
|
|
|
|
05-05-2008, 09:00 AM
|
#8 (permalink)
|
|
The Acquainted
Join Date: Nov 2007
Location: Netherlands
Posts: 113
Thanks: 11
|
@Orc I think this is pretty basic stuff so it belongs in the Beginners section
|
|
|
|
05-05-2008, 09:01 AM
|
#9 (permalink)
|
|
The Prestige
Join Date: Dec 2007
Posts: 1,044
Thanks: 193
|
Quote:
Originally Posted by sjaq
@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
|
|
|
|
05-05-2008, 09:06 AM
|
#10 (permalink)
|
|
The Prestige
Join Date: Dec 2007
Posts: 1,044
Thanks: 193
|
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
|
|
|
|
05-05-2008, 09:06 AM
|
#11 (permalink)
|
|
The Prestige
Join Date: Sep 2007
Location: Sweden, Stockholm
Posts: 1,080
Thanks: 115
|
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.. 
__________________
|
|
|
|
05-05-2008, 09:08 AM
|
#12 (permalink)
|
|
The Prestige
Join Date: Dec 2007
Posts: 1,044
Thanks: 193
|
Quote:
Originally Posted by Tanax
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
|
|
|
|
05-05-2008, 09:31 AM
|
#13 (permalink)
|
|
The Acquainted
Join Date: Nov 2007
Location: Netherlands
Posts: 113
Thanks: 11
|
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.
|
|
|
|
05-05-2008, 09:33 AM
|
#14 (permalink)
|
|
The Prestige
Join Date: Dec 2007
Posts: 1,044
Thanks: 193
|
Quote:
Originally Posted by sjaq
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
|
|
|
|
|
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
|
|
|
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|