View Full Version : preg_replace...er, What it Do?
Digital Jedi
10-27-2013, 05:44 AM
Assuming I wanted to replace a large portion of the navbar template with a custom template, let's say from <!-- nav buttons bar --> to <!-- / nav buttons bar -->, would the first part of this plugin be correct?
$template = $vbulletin->templatecache['navbar'];
$match = '<!-- nav buttons bar/.+/nav buttons bar -->';
$replace = $vbulletin->templatecache['djs_navbar'];
This is assuming that dot matches newlines. What I don't know what to do know, is what to do with the preg_replace function itself. I thought something like:
$vbulletin->templatecache['navbar'] = preg_replace($match, $replace, $template);
But until I've got the first part right, I'm just guessing. Not to mention, I have no idea which hook will work in this case.
vBNinja
10-27-2013, 06:46 AM
do you want to change it on all the pages? ... or?
--------------- Added 1382856632 at 1382856632 ---------------
also post exactly what you want to replace so i can write the regex for you
I think what you have is almost right. You need the '/' delimiters around the entire pattern, and you need an 's' modifier at the end so that the '.' will match newlines. So Try:
$match = '/<!-- nav buttons bar.+nav buttons bar -->/s';
nerbert
10-27-2013, 11:20 AM
"\s\S" matches anything, including line breaks.
/<!-- nav buttons bar[\s\S]+?nav buttons bar -->/
Digital Jedi
10-27-2013, 05:58 PM
Ah, so I had my delimiters in the wrong place. Thanks for that. I'm pretty sure I can figure out the regex, but I wasn't sure if my PHP syntax was correct to start with. So the actual preg_replace function is written correctly?
$vbulletin->templatecache['navbar'] = preg_replace($match, $replace, $template);
And, I guess, there's no way to be sure about which hook to use.
The call looks correct. I think parse_templates is a good hook to use, since you know the templates will be cached at that time.
Digital Jedi
10-27-2013, 07:01 PM
Looks like the regex works (totally forgot about [\s\S] ), and I got the navbar to disappear on global_complete and parse_templates. Yeah, parse_templates seems like the better one to use. Only thing now is the content of my custom template is not being inserted. But, at least I'm farther along than I was. Thanks again.
Yeah, I was actually thinking vb4, but in vb3 global_start works too.
Digital Jedi
10-27-2013, 08:56 PM
Interesting, I can get replace to work if I put the raw HTML in the $replace variable, but not my custom template. This line must still be wrong:
$replace = $vbulletin->templatecache['djs_navbar'];
Is your template being cached? If not you need a plugin on hook cache_templates and add the name of your template to the $globaltemplates array, or else you can call fetch_template() (although that would add an extra db query).
Digital Jedi
10-27-2013, 09:33 PM
<facepalm> No, it wasn't. I usually do that first when creating a new template for a plugin. That was the problem. Thanks, again.
Yeah, I figured you must know how to do that and probably just forgot.
vBulletin® v3.8.12 by vBS, Copyright ©2000-2024, vBulletin Solutions Inc.