Go Back   vb.org Archive > vBulletin 4 Discussion > vB4 Programming Discussions
FAQ Community Calendar Today's Posts Search

Reply
 
Thread Tools Display Modes
  #1  
Old 12-28-2012, 04:33 AM
achoo254's Avatar
achoo254 achoo254 is offline
 
Join Date: Mar 2012
Location: Vietnam
Posts: 41
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default 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
Reply With Quote
  #2  
Old 12-28-2012, 11:21 AM
kh99 kh99 is offline
 
Join Date: Aug 2009
Location: Maine
Posts: 13,185
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

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.
Reply With Quote
  #3  
Old 12-28-2012, 02:27 PM
achoo254's Avatar
achoo254 achoo254 is offline
 
Join Date: Mar 2012
Location: Vietnam
Posts: 41
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

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
Reply With Quote
  #4  
Old 12-28-2012, 03:16 PM
kh99 kh99 is offline
 
Join Date: Aug 2009
Location: Maine
Posts: 13,185
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

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).
Reply With Quote
  #5  
Old 12-28-2012, 03:36 PM
kh99 kh99 is offline
 
Join Date: Aug 2009
Location: Maine
Posts: 13,185
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

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;
Reply With Quote
  #6  
Old 12-28-2012, 04:47 PM
achoo254's Avatar
achoo254 achoo254 is offline
 
Join Date: Mar 2012
Location: Vietnam
Posts: 41
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

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
Reply With Quote
  #7  
Old 12-28-2012, 04:55 PM
kh99 kh99 is offline
 
Join Date: Aug 2009
Location: Maine
Posts: 13,185
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

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 '\'.
Reply With Quote
Благодарность от:
achoo254
  #8  
Old 12-28-2012, 05:32 PM
achoo254's Avatar
achoo254 achoo254 is offline
 
Join Date: Mar 2012
Location: Vietnam
Posts: 41
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

Perfect, thank you so so so much

--------------- 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 ?
Reply With Quote
  #9  
Old 12-28-2012, 08:18 PM
kh99 kh99 is offline
 
Join Date: Aug 2009
Location: Maine
Posts: 13,185
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

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.
Reply With Quote
  #10  
Old 12-31-2012, 06:09 PM
achoo254's Avatar
achoo254 achoo254 is offline
 
Join Date: Mar 2012
Location: Vietnam
Posts: 41
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

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>
Reply With Quote
Reply


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT. The time now is 03:30 AM.


Powered by vBulletin® Version 3.8.12 by vBS
Copyright ©2000 - 2024, vBulletin Solutions Inc.
X vBulletin 3.8.12 by vBS Debug Information
  • Page Generation 0.04870 seconds
  • Memory Usage 2,264KB
  • Queries Executed 11 (?)
More Information
Template Usage:
  • (1)SHOWTHREAD
  • (1)ad_footer_end
  • (1)ad_footer_start
  • (1)ad_header_end
  • (1)ad_header_logo
  • (1)ad_navbar_below
  • (1)ad_showthread_beforeqr
  • (1)ad_showthread_firstpost
  • (1)ad_showthread_firstpost_sig
  • (1)ad_showthread_firstpost_start
  • (19)bbcode_code
  • (1)footer
  • (1)forumjump
  • (1)forumrules
  • (1)gobutton
  • (1)header
  • (1)headinclude
  • (1)navbar
  • (3)navbar_link
  • (120)option
  • (10)post_thanks_box
  • (1)post_thanks_box_bit
  • (10)post_thanks_button
  • (1)post_thanks_javascript
  • (1)post_thanks_navbar_search
  • (1)post_thanks_postbit
  • (10)post_thanks_postbit_info
  • (10)postbit
  • (10)postbit_onlinestatus
  • (10)postbit_wrapper
  • (1)spacer_close
  • (1)spacer_open
  • (1)tagbit_wrapper 

Phrase Groups Available:
  • global
  • inlinemod
  • postbit
  • posting
  • reputationlevel
  • showthread
Included Files:
  • ./showthread.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/functions_bigthree.php
  • ./includes/class_postbit.php
  • ./includes/class_bbcode.php
  • ./includes/functions_reputation.php
  • ./includes/functions_post_thanks.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
  • showthread_start
  • showthread_getinfo
  • forumjump
  • showthread_post_start
  • showthread_query_postids
  • showthread_query
  • bbcode_fetch_tags
  • bbcode_create
  • showthread_postbit_create
  • postbit_factory
  • postbit_display_start
  • post_thanks_function_post_thanks_off_start
  • post_thanks_function_post_thanks_off_end
  • post_thanks_function_fetch_thanks_start
  • fetch_musername
  • post_thanks_function_fetch_thanks_end
  • post_thanks_function_thanked_already_start
  • post_thanks_function_thanked_already_end
  • postbit_imicons
  • bbcode_parse_start
  • bbcode_parse_complete_precache
  • bbcode_parse_complete
  • postbit_display_complete
  • post_thanks_function_can_thank_this_post_start
  • post_thanks_function_fetch_thanks_bit_start
  • post_thanks_function_show_thanks_date_start
  • post_thanks_function_show_thanks_date_end
  • post_thanks_function_fetch_thanks_bit_end
  • post_thanks_function_fetch_post_thanks_template_start
  • post_thanks_function_fetch_post_thanks_template_end
  • tag_fetchbit_complete
  • forumrules
  • navbits
  • navbits_complete
  • showthread_complete