Here's an example from one of my modifications using
preg_replace in order to make on-the-fly template changes to all templates -- including modified ones:
PHP Code:
$find = '/<tr(.*)>/';
preg_match($find, $vbulletin->templatecache['threadbit'], $match);
$vbulletin->templatecache['threadbit'] = preg_replace($find, '<tr' . $match[1] . ' id=\"vbpostrow_$thread[' . ($vbulletin->options['ajax_firstpost_firstlast'] ? 'lastpostid' : 'firstpostid') . ']\">', $vbulletin->templatecache['threadbit']);
unset($find, $match);
$find = '/\$thread\[title_editable\](\s*)<div(.*)?>/';
preg_match($find, $vbulletin->templatecache['threadbit'], $match);
$vbulletin->templatecache['threadbit'] = preg_replace($find, $match[0] . $match[1] . ' <img id=\"vbpostimg_$thread[' . ($vbulletin->options['ajax_firstpost_firstlast'] ? 'lastpostid' : 'firstpostid') . ']\" src=\"$stylevar[imgdir_button]/expand.gif\" onclick=\"return vbpost_get($thread[' . ($vbulletin->options['ajax_firstpost_firstlast'] ? 'lastpostid' : 'firstpostid') . '])\" onMouseOver=\"this.style.cursor=\'pointer\';\" />', $vbulletin->templatecache['threadbit']);
unset($find, $match);
The problem with using
str_replace is that most people have a tendency to edit their templates. Thus, the static text you're searching the template for may have been changed. Now, if the text has been removed altogether there's nothing you can do -- however, if say by default you're looking for:
And, in the users template it's changed to:
HTML Code:
<div class="alt1">
Your
str_replace is NOT going to find it:
PHP Code:
$output = str_replace('<div>', '<div id="MyID">', $text);
However, using
preg_replace WILL:
PHP Code:
$output = preg_replace('/<div(.*)>/', '<div id="MyID">', $text);
In my example code above, I use
preg_match to retain the user's variables even after inserting my own:
PHP Code:
preg_match('/<div(.*)>/', $text, $match);
$output = preg_replace('/<div(.*)>/', '<div' . $match[1] . ' id=\"MyID\">', $text);