The Arcive of Official vBulletin Modifications Site.It is not a VB3 engine, just a parsed copy! |
|
#1
|
||||
|
||||
How to random array?
2 all,
I'm don't know how to display random array in strings. Example: Code:
$display = 'hello word - hello word - hello word'; $find = ('h'); $replace = ('.$my_array .'); $my_array = ('1','2','3','4'); Code:
h1ello word - h1ello word - h1ello word Code:
h2ello word - h2ello word - h2ello word Code:
h1ello word - h2ello word - h3ello word |
#2
|
|||
|
|||
I'm not completely sure what you're askking, but you could do this:
Code:
$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: Code:
$output = str_replace(array_fill(0, count($replace), shuffle($find)), $replace, $display); |
#3
|
||||
|
||||
It's not working.
Code: 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; ?> Code:
1ello word - 1ello word - 1ello word |
#4
|
|||
|
|||
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). |
#5
|
|||
|
|||
OK, this seems to work:
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 $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): Code:
<?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; |
#6
|
||||
|
||||
thank so much!, i using code:
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; Example: Code:
h1ello word - h2ello word - h3ello word Code:
h1ello3 wo2rd - h2ello1 wo3rd - h3ello2 wo1rd |
#7
|
|||
|
|||
Maybe something like this:
Code:
<?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 |
#8
|
||||
|
||||
Perfect, thank you so so so much
--------------- Added [DATE]1356724940[/DATE] at [TIME]1356724940[/TIME] --------------- Ah, with characters 'special' like this: Code:
đ','?','?','ễ','ỹ Code:
Ch','Tr','Ph' |
#9
|
|||
|
|||
I think those should be OK, like:
Code:
'/Ch/','/Tr/','/Ph/' |
#10
|
||||
|
||||
kh99 help me,
In $post['message'] (html codes), example: Code:
line 1 <br> <br> line 2 Code:
<span style="A">line 1</span> <br> <br> <span style="B">line 2</span> |
Thread Tools | |
Display Modes | |
|
|
X vBulletin 3.8.12 by vBS Debug Information | |
---|---|
|
|
More Information | |
Template Usage:
Phrase Groups Available:
|
Included Files:
Hooks Called:
|