Quotes from the regular expression loop

Switching the debate forum code on my website, I'm going to change the way quotes are stored in the database. Now I need to create a regex to reorder already sent messages in my database.

Below is an example of how my current discussion post is stored in a database (with quotation marks in quotation marks). Note. I did this to illustrate:

Just citing a post
[quote]Text of quote #3
       [quote]Text of quote #2
              [quote]Text of quote #1
                     [name]User 1[/name]
              [/quote]
              [name]User 2[/name]
       [/quote]
       [name]User 3[/name]
[/quote]

Now I would like the first one to be rebuilt to look like this:

Just citing a post
[quote:User 3]
      Text of quote #3
      [quote:User 2]
           Text of quote #2
           [quote:User 1]
                 Text of quote #1
           [/quote]  
      [/quote]   
[/quote]

Can any of you tell me how this can be done with regex? I am using PHP.

Thanks in advance, I appreciate all your help :)

Fisher

+3
source share
4 answers

This will be done:

$input = "Just citing a post
[quote]Text of quote #3
       [quote]Text of quote #2
              [quote]Text of quote #1
                     [name]User 1[/name]
              [/quote]
              [name]User 2[/name]
       [/quote]
       [name]User 3[/name]
[/quote]";

function fix_quotes($string) {
    $regexp = '`(\s*)\[quote\]((?:[^\[]|\[(?!quote\]))*?)\[name\](.*?)\[\/name\]\s*\[\/quote\]`';
    while (preg_match($regexp, $string)) {
        $string = preg_replace_callback($regexp, function($match) {
            return $match[1] . '[quote:' . $match[3] . ']' . trim(fix_quotes($match[2])) . $match[1] . '[/quote]';
        }, $string);
    }
    return $string;
}

echo fix_quotes($input);

Results in:

Just citing a post
[quote:User 3]Text of quote #3
       [quote:User 2]Text of quote #2
              [quote:User 1]Text of quote #1
              [/quote]
       [/quote]
[/quote]

: , joelhardi , , :)

+1

. :

function reformat($str) {
  while (preg_match('#\[quote\](.+)\[name\](.+)\[/name\]\s*\[/quote\]#Us',
         $str, 
         $matches)) {
    $str = str_replace($matches[0], 
                       '[quote:'.$matches[2].']'.$matches[1].'[/quote]',
                       $str);
  }
  return $str; 
}

:

$before = "Just citing a post
  [quote]Text of quote #3
    [quote]Text of quote #2
      [quote]Text of quote #1
        [name]User 1[/name]
      [/quote]
      [name]User 2[/name]
    [/quote]
    [name]User 3[/name]
  [/quote]";

echo reformat($before);

:

Just citing a post
  [quote:User 3]Text of quote #3
    [quote:User 2]Text of quote #2
      [quote:User 1]Text of quote #1
        [/quote]
      [/quote]
    [/quote]
+1

. , , XML, regex XML. , .

, XML. , , , . :

: , , XML:

<quote src="User 3">
      Text of quote #3
      <quote src="User 2">
           Text of quote #2
           <quote src="User 1">
                 Text of quote #1
           </quote>  
      </quote>   
</quote>
0

Due to the complexity here (you will need the conventions as well as the "Match / Replace All" functionality), I would recommend not doing this only in Regex. Use the powerful Regex programming language and combine Regex with that language to do what you want. I recommend PHP.

0
source

All Articles