PDA

View Full Version : How to random array?


achoo254
12-28-2012, 04:33 AM
2 all,

I'm don't know how to display random array in strings. Example:

$display = 'hello word - hello word - hello word';
$find = ('h');
$replace = ('.$my_array .');
$my_array = ('1','2','3','4');

when code display:
h1ello word - h1ello word - h1ello word

Re-load page:
h2ello word - h2ello word - h2ello word

So, how to display like this:
h1ello word - h2ello word - h3ello word

Please help me, thanks :)

kh99
12-28-2012, 11:21 AM
I'm not completely sure what you're askking, but you could do this:

$display = 'hello word - hello word - hello word';
$find = 'h';
$replace = array('1','2','3','4');
$output = str_replace(array_fill(0, count($replace), $find), $replace, $display);


or if you want random replacements:
$output = str_replace(array_fill(0, count($replace), shuffle($find)), $replace, $display);

That will use them in a random order. If instead you want each replacement to have the same chance of being used each time (in other words, you could get h1ello word - h2ello word - h1ello word) then I think you need something a little more complicated.

achoo254
12-28-2012, 02:27 PM
It's not working. :(

Code:
<?php
$display = 'hello word - hello word - hello word';
$find = 'h';
$replace = array('1','2','3','4');
$output = str_replace(array_fill(0, count($replace), $find), $replace, $display);
echo $output;
?>

Display:
1ello word - 1ello word - 1ello word

kh99
12-28-2012, 03:16 PM
Oh, oops. Of course that will always just use the first 'h' to match so that's why it doesn't work.

I'll have to think about it more (unless someone else comes up with something).

kh99
12-28-2012, 03:36 PM
OK, this seems to work:

<?php
$display = 'hello word - hello word - hello word';
global $find, $replace;
$find = 'h';
$replace = array('1','2','3','4');
$output = preg_replace_callback("/$find/", create_function(
'$matches',
'global $find, $replace; return $matches[0] . $replace[array_rand($replace)];'
),
$display
);

echo $output;



It selects from the $replace array at random but there's nothing stopping it from repeating (you didn't say if you wanted to use them all in random order or if repeating was OK).

Edit: here's another version that's not random but uses all the values (like what you originally posted):

<?php
$display = 'hello word - hello word - hello word';
$find = 'h';
$replace = array('1','2','3','4');
$rptr = 0;
$output = preg_replace_callback("/$find/", create_function(
'$matches',
'global $find, $replace, $rptr; $ret = $matches[0] . $replace[$rptr]; $rptr = ($rptr + 1) % count($replace); return $ret;'
),
$display
);

echo $output;

achoo254
12-28-2012, 04:47 PM
thank so much!, i using code:
<?php
$display = 'hello word - hello word - hello word';
global $find, $replace;
$find = 'h';
$replace = array('1','2','3','4');
$output = preg_replace_callback("/$find/", create_function(
'$matches',
'global $find, $replace; return $find . array_rand($replace);'
),
$display
);

echo $output;

How to $find is array?

Example:
h1ello word - h2ello word - h3ello word

to:
h1ello3 wo2rd - h2ello1 wo3rd - h3ello2 wo1rd

kh99
12-28-2012, 04:55 PM
Maybe something like this:

<?php
$display = 'hello word - hello word - hello word';
global $find, $replace;
$find = array('/h/', '/o/');
$replace = array('1','2','3','4');
$output = preg_replace_callback($find, create_function(
'$matches',
'global $replace; return $matches[0] . $replace[array_rand($replace)];'
),
$display
);

echo $output;


There was an error in the previous code I posted that I didn't notice because the replace array is all numbers, but array_rand returns a key so you have to use the result in $replace[].

Also, the $find array is search patterns, which is why they start and end with '/'. If you're searching for strings with characters that are 'special' in patterns, then you might have to escape them with '\'.

achoo254
12-28-2012, 05:32 PM
Perfect, thank you so so so much :D

--------------- Added 1356724940 at 1356724940 ---------------

Ah, with characters 'special' like this:
đ','?','?','ễ','ỹ

Or this:
Ch','Tr','Ph'

How to do in array $find ?

kh99
12-28-2012, 08:18 PM
I think those should be OK, like:

'/Ch/','/Tr/','/Ph/'

The special characters are like []-.+*?^$, maybe some others. Also '/' but you can actually use any character instead of '/' as a delimiter.

achoo254
12-31-2012, 06:09 PM
kh99 help me,

In $post['message'] (html codes), example:
line 1
<br>
<br>
line 2

How to change $post['message'] tags html like this:
<span style="A">line 1</span>
<br>
<br>
<span style="B">line 2</span>