PDA

View Full Version : str_replace help please


HMBeaty
07-05-2011, 12:08 AM
Ok, this is my 1st time playing around with str_replace and I'm obviously doing SOMETHING wrong as my code isn't working lol

Basically what I'm trying to do, is turn this (https://vborg.vbsupport.ru/showthread.php?t=240389) into an automatic template edit, and eventually do the same with most of my personal modifications which haven't and won't be released here. So no need to worry about if user x installs on a custom style, he/she may run into problems later. :)

Anyway, on with the codes :D

Currently, I have this in a plugin
$find = '<vb:if condition=\"$post['signature']\">';
$replace = '<vb:if condition=\"$post['signature'] AND !$vboptions['shownsigs'][$post[userid]] AND $vboptions['shownsigs'][$post[userid]] = true\">';
$output = str_replace($find,$replace.$find, $output);Which probably isn't right, but a lot of the threads I've searched through for vB 4 str_replace are incomplete as far as an official WORKING answer.

Now, with the code above, I've tried using the hook locations:
global_start
showthread_start
parse_templates
showthread_postbit_create
postbit_display_start
postbit_display_complete
showthread_completeWhich seem to be the most logical ones to use for this modification, but it still isn't working. So, to me, that HOPEFULLY narrows it down to my code not being correct.

So, my next question is, what's wrong with it? :confused:

/ str_replace noob :D

Adrian Schneider
07-05-2011, 12:12 AM
Templates get compiled when you save into actual PHP code, and evaluated before it gets to global_shutdown.

global_start (or vb4 equivalent)
echo $vbulletin->templatecache['TEMPLATE_NAME']; exit;See what that prints out, and you'll have to do str_replace() on that variable, rather than $output.

$vbulletin->templatecache['postbit'] = str_replace(
$find,
$replace,
$vbulletin->templatecache['postbit']
);

--------------- Added 1309828392 at 1309828392 ---------------

Alternatively, you could probably do it all in PHP.

Once you've rendered it once, set $post['signature'] = '';

HMBeaty
07-05-2011, 12:30 AM
Templates get compiled when you save into actual PHP code, and evaluated before it gets to global_shutdown.

global_start (or vb4 equivalent)
echo $vbulletin->templatecache['TEMPLATE_NAME']; exit;See what that prints out, and you'll have to do str_replace() on that variable, rather than $output.
That gave me a blank white page
$vbulletin->templatecache['postbit'] = str_replace(
$find,
$replace,
$vbulletin->templatecache['postbit']
);--------------- Added 04 Jul 2011 at 21:13 ---------------

Alternatively, you could probably do it all in PHP.

Once you've rendered it once, set $post['signature'] = '';
And you lost me there lol

Adrian Schneider
07-05-2011, 12:39 AM
Move that first plugin to global_complete (for example's sake). I always forget that the global_start one has output buffering which would give you the blank page.

Once you see the output from a compiled template, it will give you an idea on how to actually figure out what to put in $find and $replace.


For the alternate method, upon successfully displaying the signature once, you can empty that variable so it won't display again, rather than relying on the template edit.

postbit_display_start
// if signature exists, and cached (already displayed on page)
if ($this->post['signature'] and $this->cache['sig'][$this->post['userid']]) {
$this->post['signature'] = '';
$this->post['showsignature'] = false;
}Not tested, but may work.

HMBeaty
07-05-2011, 12:51 AM
Move that first plugin to global_complete (for example's sake). I always forget that the global_start one has output buffering which would give you the blank page.

Once you see the output from a compiled template, it will give you an idea on how to actually figure out what to put in $find and $replace.
Sorry, guess I should've mentioned I tried global_complete too, and that too gave a blank page.

For the alternate method, upon successfully displaying the signature once, you can empty that variable so it won't display again, rather than relying on the template edit.

postbit_display_start
// if signature exists, and cached (already displayed on page)
if ($this->post['signature'] and $this->cache['sig'][$this->post['userid']]) {
$this->post['signature'] = '';
$this->post['showsignature'] = false;
}Not tested, but may work.
Just tested and this DOES work. Thanks. :) Now just need an updated tutorial for str_replace for vB 4 lol

Boofo
07-05-2011, 01:57 AM
Look how I did it here:

https://vborg.vbsupport.ru/showthread.php?t=259687

I did the mod for this for vb4.

MaryTheG(r)eek
07-05-2011, 06:56 AM
I don't think that:

$output = str_replace($find,$replace.$find, $output);

is correct. You evaluate an expression there which maybe will cause endless loop by having $find there, which is also the first argument. I haven't seen all your code, but if you want to use that expression is better to add another variable before. eg:

$mynewreplace = $replace.$find;
$output = str_replace($find,$mynewreplace, $output);


Maria

Boofo
07-05-2011, 07:20 AM
In vb4, find doesn't work with if conditions very well. That is why your code below didn't work.

$find = '<vb:if condition=\"$post['signature']\">';