TalkPHP

TalkPHP (http://www.talkphp.com/forums.php)
-   Absolute Beginners (http://www.talkphp.com/absolute-beginners/)
-   -   Randomize code (http://www.talkphp.com/absolute-beginners/2758-randomize-code.html)

Tanax 05-05-2008 07:26 AM

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? :-P

Orc 05-05-2008 07:48 AM

Quote:

Originally Posted by Tanax (Post 14278)
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? :-P

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.

sjaq 05-05-2008 08:28 AM

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); 


Tanax 05-05-2008 08:37 AM

Thanks!
Is it possible to also have it with numbers? :O :-D

Orc 05-05-2008 08:40 AM

Quote:

Originally Posted by Tanax (Post 14281)
Thanks!
Is it possible to also have it with numbers? :O :-D

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;
?>


sjaq 05-05-2008 08:52 AM

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

Orc 05-05-2008 08:53 AM

By the way, why is this in Absolute Beginners, I thought Tanax was more than a just a Beginner? :P

sjaq 05-05-2008 09:00 AM

@Orc I think this is pretty basic stuff so it belongs in the Beginners section

Orc 05-05-2008 09:01 AM

Quote:

Originally Posted by sjaq (Post 14285)
@Orc I think this is pretty basic stuff so it belongs in the Beginners section

heh. what do you mean by basic?

Orc 05-05-2008 09:06 AM

BY the way, heres another method
PHP Code:

<?php

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


echo 
$string;



?>


Tanax 05-05-2008 09:06 AM

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.. :-P

Orc 05-05-2008 09:08 AM

Quote:

Originally Posted by Tanax (Post 14288)
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.. :-P

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

sjaq 05-05-2008 09:31 AM

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.

Orc 05-05-2008 09:33 AM

Quote:

Originally Posted by sjaq (Post 14290)
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.


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

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0