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

Using Regex to replace per new line

Discussion in 'XenForo Development Discussions' started by silence, Sep 4, 2013.

  1. silence

    silence Well-Known Member

    I'm doing as specified by this thread, except going into the nitty gritty of it:
    http://xenforo.com/community/threads/bit-of-an-immature-request.42852/

    So I've extended XenForo_BbCode_Formatter_Base and am doing the following:

    PHP:
    <?php

    class GreenText_BbCode_Formatter_Base extends XFCP_GreenText_BbCode_Formatter_Base
    {
        public function 
    filterString($string, array $rendererStates)
        {
            
    $parent parent::filterString($string$rendererStates);
            
    $pattern "/(\n|^)>(.*?)(\n|$)/";

            if (
    preg_match($pattern$string))
            {
                
    $replacement '<span style="color: #789922">$i</span>';
                
    $string nl2br(preg_replace($pattern$replacement$string));
                
    Zend_Debug::dump($string);
            }

            return 
    $string;
        }
    }
    However, I can't for the life of me figure this out! I get this in my posts:
    3QA24gy.png
    Should I summon the gods of Zeus or...
     
  2. Daniel Hood

    Daniel Hood Well-Known Member

    You use $1 not $i; I'm also fairly certain it won't work if it's in ' instead of " like you have it.
     
    silence likes this.
  3. silence

    silence Well-Known Member

    I got back into this, and got this after a while but I can't get a damn response out of my script:
    PHP:
            $parent parent::renderHtml();

            
    $posts $this->_params['posts'];

            foreach (
    $posts AS &$post)
            {
                
    $pattern '/(\n|^)\s*>.*?(?=\n|$)/';
                
    $post['messageHtml'] = preg_replace($pattern"<span class=\"famamama\">&#62;$1</span>"$post['messageHtml']);
            }

            
    $this->_params['posts'] = $posts;

            return 
    $parent;
     
  4. Marcus

    Marcus Well-Known Member

    In your script you return $parent. You should do something with the parameter, otherwise your script starts getting $parent = ..., then your script is doing something. And after that it is returning the virgin untouched $parent back.

    As an example: This would return an empty string:

    $parent = '';
    return $parent;
     
  5. silence

    silence Well-Known Member

    I don't follow D:
     
  6. Marcus

    Marcus Well-Known Member

    Imagine you will give me a letter.

    Then I take your letter, and make breakfast.

    After that I return you the letter.

    Will the letter be a different letter than before? No :D

    Your addon starts with
    $parent = parent::renderHtml();

    Imagine $parent is the letter.

    Then you make something in your script.

    After you decide you want to finish, you return $parent. Untouched. Is it a different $parent than the one you fetched at the start of your script? No :D
     
  7. Marcus

    Marcus Well-Known Member

    $this->_params['posts'] = $posts;

    you can change that to:

    $parent->_params['posts'] = $posts;

    But I do not know the function you are manipulating and the class you are extending.
     
  8. silence

    silence Well-Known Member

    PHP:
    <?php

    class Fafafafa_ViewPublic_Thread_View extends XFCP_Fafafafa_ViewPublic_Thread_View
    {
        public function 
    renderHtml()
        {
            
    $parent parent::renderHtml();

            
    $posts $this->_params['posts'];

            foreach (
    $posts AS &$post)
            {
                
    $pattern '/(\n|^)\s*>.*?(?=\n|$)/';
                
    $post['messageHtml'] = preg_replace($pattern"<span class=\"Fafafafa\">&#62;$1</span>"$post['messageHtml']);
            }

            
    $this->_params['posts'] = $posts;

            return 
    $parent;
        }
    }
     
  9. Marcus

    Marcus Well-Known Member

    PHP:
      public function renderHtml()
     {
    $parent parent::renderHtml();
    $posts $this->_params['posts'];

     foreach (
    $posts AS &$post)
     {
    $pattern '/(\n|^)\s*>.*?(?=\n|$)/';
    $post['messageHtml'] = preg_replace($pattern"<span class=\"Fafafafa\">&#62;$1</span>"$post['messageHtml']);
     }
    $parent->_params['posts'] = $posts;

     return 
    $parent;
     }
     
  10. silence

    silence Well-Known Member

    1. Object of class stdClass could not be converted to string in /home/xxxxxxx/public_html/xenforo/internal_data/templates/S.33,L.1,PAGE_CONTAINER.php, line 1384
     
  11. Marcus

    Marcus Well-Known Member

    Try this
    PHP:
    public function renderHtml()
    {
    $parent parent::renderHtml()
    ;
    $posts $this->_params['posts'];

    foreach (
    $posts AS &$post)
    {
    $pattern '/(\n|^)\s*>.*?(?=\n|$)/';
    $post['messageHtml'] = preg_replace($pattern"<span class=\"Fafafafa\">&#62;$1</span>"$post['messageHtml']);
    }
    $parent->params['posts'] = $posts;

    return 
    $parent;
    }
     
  12. silence

    silence Well-Known Member

    I'm confused it looks exactly the same D:
     
  13. Marcus

    Marcus Well-Known Member

    Did you try it? The problem is about getting $parent->params back.
     
  14. silence

    silence Well-Known Member

    The $parent->params['posts'] = $posts; causes the error I posted above :(
     
  15. Marcus

    Marcus Well-Known Member

    $parent->_params['posts'] = $posts; caused the error.

    That's why I suggested you to write $parent->params['posts'] = $posts;
     
  16. silence

    silence Well-Known Member

    I don't think it's t
    Yes and I get the same error.
     
  17. Marcus

    Marcus Well-Known Member

    OK, you have to know what's in the $parent variable, only then you can manipulate it.

    you can do print_r($parent) inside your code and take a look at that.
     
  18. silence

    silence Well-Known Member

    It prints NULL
     
  19. silence

    silence Well-Known Member

    W/e I figured it out. Extend XenForo_BbCode_Formatter_Base and use the function filterString to do the dirty work. Thanks for the bump in the right direction though :D
     
  20. Marcus

    Marcus Well-Known Member

    if you are on debug mode, you can also use ZEND_DEBUG::dump($variable); I think this is much better than print_r.

    Great you figured it out. It's very important to know what's inside the variables, I usually just output variables inside the original code so I know where to start my own addons.
     

Share This Page