vb.org Archive

vb.org Archive (https://vborg.vbsupport.ru/index.php)
-   vB3 Programming Discussions (https://vborg.vbsupport.ru/forumdisplay.php?f=15)
-   -   text replacements help (https://vborg.vbsupport.ru/showthread.php?t=135643)

paul41598 01-05-2007 07:56 PM

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

nico_swd 01-05-2007 08:51 PM

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.

paul41598 01-05-2007 10:37 PM

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)

nico_swd 01-05-2007 10:59 PM

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


paul41598 01-05-2007 11:30 PM

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


Dismounted 01-06-2007 02:43 AM

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

paul41598 01-06-2007 12:37 PM

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


All times are GMT. The time now is 02:35 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.01055 seconds
  • Memory Usage 1,738KB
  • 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
  • (2)bbcode_code_printable
  • (4)bbcode_php_printable
  • (1)footer
  • (1)gobutton
  • (1)header
  • (1)headinclude
  • (6)option
  • (1)post_thanks_navbar_search
  • (1)printthread
  • (7)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