Where to run preg_replace on message content?

Paul B

XenForo moderator
Staff member
My add-on retrieves the content of the first post and in the template I use {xen:helper snippet, $post.message, $xenOptions.ctaFtMaxContentLength} to trim to a max number of characters.

Using the helper also conveniently removes bbcode tags.

However, XenForo\Helper\String.php line 409 is:
PHP:
$string = preg_replace('#\[(attach|media|img)[^\]]*\].*\[/\\1\]#siU', '[\\1]', $string);
Which avoids the problem of having empty previews in the event that there are just attachments, media and images; they are replaced with [ATTACH], [media] and [IMG] respectively.

However I want to remove those placeholder tags using:
PHP:
$message = preg_replace('/\[(attach|media|img)\]/siU', '', $post['message']);

Using that in the controller doesn't work as doing a Zend_Debug::dump($message); shows that the content is actually the original post content, with all bbcode, when it is passed to the template.

So where and how do I run the preg_replace to strip out those placeholder tags?
 
Well, are you attempting to accomplish this within a template (well, after you send a message's content) or before you insert into the featured thread table?
 
In the template, before inserting into the table.

Clicking the Feature Thread link retrieves the first post content, allows it to be edited and then it is saved to a new table.

Like so:
upload_2013-11-14_22-40-13.webp

I want to strip out those remaining tags from that content.
 
The answer is -- and I do apologise I worked it out ages ago but been away from my laptop -- to do all of this in the controller action.

Template helpers are just PHP, you can call the helper in your controller action, then do the preg_replace, then perform any other manipulations on the string.

You then don't need to use the helper in the template because by that point it has already been snipped, trimmed and whatever else you want to do.
 
Thanks Chris.

I was wondering if I could run the helper in the controller but wasn't sure.

So, the steps are to remove the helper and options from the template, move them to the controller and then run the preg_replace on the output.
 
Basically, you'll call the helper, do any snippets, and then you can return your modified content to the template for display. :)
 
I got the helper function working in the controller using (after finally changing the param in the template from $post.message to $message :rolleyes:):
PHP:
$chars = XenForo_Application::get('options')->ctaFtMaxContentLength;

$content = XenForo_Template_Helper_Core::helperSnippet($post['message'], $chars);


However, the next step which is the preg_replace isn't having any effect.
PHP:
$message = preg_replace('/\[(attach|media|img)\]/siU', '', $content);
Can anyone see anything obviously wrong with this?


It should basically take the output from the helper and remove the remaining empty tags.
 
Disregard.

It was working, I just had some erroneous code in the file.

All sorted now :)
 
A result of removing the blank bbcode tags was that they were replaced with blank lines.
So this is the final code which first runs the message content through the helper utilising the max character option in the ACP, then removes the blank tags, and finally the blank rows.

PHP:
$chars = XenForo_Application::get('options')->ctaFtMaxContentLength;
$content = XenForo_Template_Helper_Core::helperSnippet($post['message'], $chars);
$output = preg_replace('/\[(attach|media|img)\]/siU', '', $content);
$message = preg_replace('/^\n+|^[\t\s]*\n+/m', '', $output);

What that does is change this:
before.webp

To this:
step1.webp

To this:
step2.webp


And finally to this:
after.webp
 
Last edited:
Back
Top Bottom