php - BBCode parse quotes

997

Okay, I'm currently trying to create bbcodes for my home made forum. It worked good, until I started with quotes. This is my function:

public function forum_parse ($string)
{
  global $core, $path;
  $string = $this -> normal_parse ($string);

  $search = '/\[quote=([A-z0-9 -_\'"]+);([0-9]+)\](.*)\[\/quote\]/is';
  $replace = '<div ><p ><a href="' . $path . 'forum/viewtopic?p=$2">' . WRITTEN_BY . ' $1</a></p><p >$3</p></div>';

  return preg_replace ($search, $replace, $string);
}

It works good when there's one quote per post, but when there's more, the problems begin. It obviously doesn't start from the root quote and choose accurate end tags by itself. And I' not experienced with RegEx enough to fix it. Any help? :/

440

Answer

Solution:

.* will consume as much as it can. Changing it to.*? will make it consume as little as it can, which should fix the problem ... just as long as you don't consider nested quotes.

A better solution, I believe, would be to use the answers to this post instead of inventing your own.

People are also looking for solutions to the problem: php - Sorting with combobox?

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.