• 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

silence

Well-known member
#1
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...
 

silence

Well-known member
#3
You use $1 not $i; I'm also fairly certain it won't work if it's in ' instead of " like you have it.
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;
 

Marcus

Well-known member
#4
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;
 

silence

Well-known member
#5
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;
I don't follow D:
 

Marcus

Well-known member
#6
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
 

Marcus

Well-known member
#7
$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.
 

silence

Well-known member
#8
$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.
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;
    }
}
 

Marcus

Well-known member
#9
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;
 }
 

silence

Well-known member
#10
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;
}
  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
 

Marcus

Well-known member
#11
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;
}
 

silence

Well-known member
#12
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;
}
I'm confused it looks exactly the same D:
 

Marcus

Well-known member
#15
$parent->_params['posts'] = $posts; caused the error.

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

Marcus

Well-known member
#17
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.
 

silence

Well-known member
#19
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.
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
 

Marcus

Well-known member
#20
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.