1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Where to run preg_replace on message content?

Discussion in 'XenForo Development Discussions' started by Brogan, Nov 14, 2013.

  1. Brogan

    Brogan 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:
    $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:
    $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?
  2. Jeremy

    Jeremy Well-Known Member

    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?
  3. Brogan

    Brogan XenForo Moderator Staff Member

    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:

    I want to strip out those remaining tags from that content.
  4. Chris D

    Chris D XenForo Developer Staff Member

    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.
    Jeremy and Brogan like this.
  5. Brogan

    Brogan XenForo Moderator Staff Member

    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.
  6. Jeremy

    Jeremy Well-Known Member

    Basically, you'll call the helper, do any snippets, and then you can return your modified content to the template for display. :)
  7. Brogan

    Brogan XenForo Moderator Staff Member

    I got the helper function working in the controller using (after finally changing the param in the template from $post.message to $message :rolleyes:):
    $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.
    $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.
  8. Brogan

    Brogan XenForo Moderator Staff Member


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

    All sorted now :)
  9. Brogan

    Brogan XenForo Moderator Staff Member

    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.

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

    To this:

    To this:

    And finally to this:
    Last edited: Nov 15, 2013
    Jeremy, MattW and Chris D like this.

Share This Page