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

Reply
 
Thread Tools Display Modes
  #1  
Old 01-05-2007, 07:56 PM
paul41598's Avatar
paul41598 paul41598 is offline
 
Join Date: Jun 2004
Location: MI
Posts: 732
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default text replacements help

I have this code below...but I also want to have multiple replacements. Like {userid} for example. How can I add multiple replacements to this line of code?

Code:
$test = @str_replace("{username}", $vbulletin->userinfo['username'], $message);
I tried doing a

Code:
$test = @str_replace("{username}", $vbulletin->userinfo['username'], $message);
$test = @str_replace("{userid}", $vbulletin->userinfo['userid'], $message);

but it didnt work, and still only outputted the userid and not both
Reply With Quote
  #2  
Old 01-05-2007, 08:51 PM
nico_swd's Avatar
nico_swd nico_swd is offline
 
Join Date: Dec 2005
Location: Spain
Posts: 170
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

Because you use $message in the second source string. Use $test there.

PHP Code:
$test = @str_replace("{username}"$vbulletin->userinfo['username'], $message);
$test = @str_replace("{userid}"$vbulletin->userinfo['userid'], $test); 
$message remains the same after the first replacement and $test gets overwritten with the second replacement.
Reply With Quote
  #3  
Old 01-05-2007, 10:37 PM
paul41598's Avatar
paul41598 paul41598 is offline
 
Join Date: Jun 2004
Location: MI
Posts: 732
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

and what if I wanna add a third and fourth, how does the theory apply? New variables each time? Because I wanna be able to use multiple replacements like username, userid, all in the same variable...from the text field I made up in the product options, I made.

You've gotta be able to do it all in one line...(peice of code)
Reply With Quote
  #4  
Old 01-05-2007, 10:59 PM
nico_swd's Avatar
nico_swd nico_swd is offline
 
Join Date: Dec 2005
Location: Spain
Posts: 170
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

No, I'll try to explain better.

The main text is held in the variable $message.
We create a new variable called $test, which is equal to $message, just with the replaced words. So we want to replace more words, we'll use the $test variable as third argument in str_replace() as $test contains the variables we replaced first. We want these plus the ones we're gonna replace now.

Do you understand? $message remains untouched as we assign the NEW text to a different variable. So use this new variable for the other replacements too.

PHP Code:

$test 
= @str_replace("{username}"$vbulletin->userinfo['username'], $message);
$test = @str_replace("{userid}"$vbulletin->userinfo['userid'], $test);
$test = @str_replace("{blah}"$vbulletin->userinfo['userid'], $test);
$test = @str_replace("{blah2}"$vbulletin->userinfo['userid'], $test);

//... 

Another option is using arrays as arguments.

PHP Code:
$replacements = array(
   
'{username}' => $vbulletin->userinfo['username'],
   
'{userid}'   => $vbulletin->userinfo['userid'],
   
'{blah}'     => $vbulletin->userinfo['blah']
   
// ...
);

$test str_replace(array_keys($replacements), array_values($replacements), $message); 
Reply With Quote
  #5  
Old 01-05-2007, 11:30 PM
paul41598's Avatar
paul41598 paul41598 is offline
 
Join Date: Jun 2004
Location: MI
Posts: 732
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

Nico, that makes alot more sense, I really appreciate the explaination and code examples. It works! Thanks again.

One more issue...how can I retrieve the logged in users usergroup title? The below wont work at all...nor will other combinations I've tried...

PHP Code:
$test = @str_replace("{blah2}"$vbulletin->usergroupcache['title'], $test); 
Reply With Quote
  #6  
Old 01-06-2007, 02:43 AM
Dismounted's Avatar
Dismounted Dismounted is offline
 
Join Date: Jun 2005
Location: Melbourne, Australia
Posts: 15,047
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

vBulletin has a built-in function to do this. Just use {1}, {2}, and so-forth as the replacement.

http://members.vbulletin.com/api/vBu...nstruct_phrase
Reply With Quote
  #7  
Old 01-06-2007, 12:37 PM
paul41598's Avatar
paul41598 paul41598 is offline
 
Join Date: Jun 2004
Location: MI
Posts: 732
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

Well, what I have is working fine...just cant grab the usergroups title, which may be a seperate issue. Maybe I'll have to do a query or something
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 02:42 PM.


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.06584 seconds
  • Memory Usage 2,230KB
  • 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
  • (2)bbcode_code
  • (4)bbcode_php
  • (1)footer
  • (1)forumjump
  • (1)forumrules
  • (1)gobutton
  • (1)header
  • (1)headinclude
  • (1)navbar
  • (3)navbar_link
  • (120)option
  • (7)post_thanks_box
  • (7)post_thanks_button
  • (1)post_thanks_javascript
  • (1)post_thanks_navbar_search
  • (7)post_thanks_postbit_info
  • (7)postbit
  • (7)postbit_onlinestatus
  • (7)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
  • post_thanks_function_fetch_thanks_end
  • post_thanks_function_thanked_already_start
  • post_thanks_function_thanked_already_end
  • fetch_musername
  • postbit_imicons
  • bbcode_parse_start
  • bbcode_parse_complete_precache
  • bbcode_parse_complete
  • postbit_display_complete
  • post_thanks_function_can_thank_this_post_start
  • tag_fetchbit_complete
  • forumrules
  • navbits
  • navbits_complete
  • showthread_complete