PDA

View Full Version : text replacements help


paul41598
01-05-2007, 07:56 PM
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?


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


I tried doing a


$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.


$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.



$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.


$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...

$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/vBulletin/_includes_functions_php.html#functionconstruct_phr ase

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