vb.org Archive

vb.org Archive (https://vborg.vbsupport.ru/index.php)
-   vB4 Programming Discussions (https://vborg.vbsupport.ru/forumdisplay.php?f=252)
-   -   How to random array? (https://vborg.vbsupport.ru/showthread.php?t=293355)

achoo254 12-28-2012 04:33 AM

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

when code display:
Code:

h1ello word - h1ello word - h1ello word
Re-load page:
Code:

h2ello word - h2ello word - h2ello word
So, how to display like this:
Code:

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:

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

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:

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;


achoo254 12-28-2012 04:47 PM

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;

How to $find is array?

Example:
Code:

h1ello word - h2ello word - h3ello word
to:
Code:

h1ello3 wo2rd - h2ello1 wo3rd - h3ello2 wo1rd

kh99 12-28-2012 04:55 PM

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 12-28-2012 05:32 PM

Perfect, thank you so so so much :D

--------------- Added [DATE]1356724940[/DATE] at [TIME]1356724940[/TIME] ---------------

Ah, with characters 'special' like this:
Code:

đ','?','?','ễ','ỹ
Or this:
Code:

Ch','Tr','Ph'
How to do in array $find ?

kh99 12-28-2012 08:18 PM

I think those should be OK, like:

Code:

'/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:
Code:

line 1
<br>
<br>
line 2

How to change $post['message'] tags html like this:
Code:

<span style="A">line 1</span>
<br>
<br>
<span style="B">line 2</span>



All times are GMT. The time now is 03:20 PM.

Powered by vBulletin® Version 3.8.12 by vBS
Copyright ©2000 - 2025, vBulletin Solutions Inc.

X vBulletin 3.8.12 by vBS Debug Information
  • Page Generation 0.01028 seconds
  • Memory Usage 1,739KB
  • Queries Executed 10 (?)
More Information
Template Usage:
  • (1)ad_footer_end
  • (1)ad_footer_start
  • (1)ad_header_end
  • (1)ad_header_logo
  • (1)ad_navbar_below
  • (19)bbcode_code_printable
  • (1)footer
  • (1)gobutton
  • (1)header
  • (1)headinclude
  • (6)option
  • (1)post_thanks_navbar_search
  • (1)printthread
  • (10)printthreadbit
  • (1)spacer_close
  • (1)spacer_open 

Phrase Groups Available:
  • global
  • postbit
  • showthread
Included Files:
  • ./printthread.php
  • ./global.php
  • ./includes/init.php
  • ./includes/class_core.php
  • ./includes/config.php
  • ./includes/functions.php
  • ./includes/class_hook.php
  • ./includes/modsystem_functions.php
  • ./includes/class_bbcode_alt.php
  • ./includes/class_bbcode.php
  • ./includes/functions_bigthree.php 

Hooks Called:
  • init_startup
  • init_startup_session_setup_start
  • init_startup_session_setup_complete
  • cache_permissions
  • fetch_threadinfo_query
  • fetch_threadinfo
  • fetch_foruminfo
  • style_fetch
  • cache_templates
  • global_start
  • parse_templates
  • global_setup_complete
  • printthread_start
  • bbcode_fetch_tags
  • bbcode_create
  • bbcode_parse_start
  • bbcode_parse_complete_precache
  • bbcode_parse_complete
  • printthread_post
  • printthread_complete