Removing nested bbcode (quotes) in PHP

I am trying to remove a nested quote from my message board, but I am having some problems.

Input Example:

[quote author = personX link = topic = 12.msg1910 # msg1910 date = 1282745641]

[quote author=PersonY link=topic=12.msg1795#msg1795 date=1282727068]

The message in the original quote

[/quote]

Second message quoting the first

[/quote]

[quote author = PersonZ link = topic = 1.msg1 # msg1 date = 1282533805]

Random Third Quote

[/quote]

Output example

[quote author = personX link = topic = 12.msg1910 # msg1910 date = 1282745641]

Post in the second quote

[/quote]

[quote author = PersonZ link = topic = 1.msg1 # msg1 date = 1282533805]

Random Third Quote

[/quote]

As you can see, the attached quote (the original message) is deleted along with the quote tags.

I can’t figure it out.

When i try

$toRemove = '(\\[)(quote)(.*?)(\\])';
$string = $txt;
$found = 0; echo preg_replace("/($toRemove)/e", '$found++ ? \'\' : \'$1\'', $string);

It removes every occurrence of the quote tag except the first,

But when I deploy the code to:

$toRemove = '(\\[)(quote)(.*?)(\\])(.*?)(\\[\\/quote\\])';
$string = $txt;
$found = 0; echo preg_replace("/($toRemove)/e", '$found++ ? \'\' : \'$1\'', $string); 

He does nothing at all.

Any ideas on this?


Edit:

Thank you for your help, Haji.

I'm still having a problem.

While loop around

while ( $input = preg_replace_callback( '~\[quoute.*?\[/quote\]~i', 'replace_callback', $input ) ) {
// replace every occurence
}

, ( u quoute) .

,

$input = preg_replace_callback( '/\[quote(.*?)/i', 'replace_callback', $input );

,

$input = preg_replace_callback( '/\[quote(.*?)\[\/quote\]/i', 'replace_callback', $input );

.

, undo_replace, , . , sha1, , .

, :

$cache = array();
$input = $txt;

function replace_callback( $matches ) {
    global $cache;
    $hash = sha1( $matches[0] );
    $cache["hash"] = $matches[0];
    return "REPLACE:$hash";
}



// replace all quotes with placeholders
$input = preg_replace_callback( '/\[quote(.*?)\[quote\]/i', 'replace_callback', $input );

function undo_replace( $matches ) {
    global $cache;
    return $cache[$matches[1]];
}

// restore the outer most quotes
$input = preg_replace_callback( '~REPLACE:[a-f0-9]{40}~i', 'undo_replace', $input );

// remove the references to the inner quotes
$input = preg_replace( '~REPLACE:[a-f0-9]{40}~i', '', $input );

echo $input;

:)

+3
2

, , :

'$found++ ? \'\' : \'$1\''

$found undefined false, $1. $ 1 (undefined + 1 = 1), , , . , , , "".

, -

$cache = array();

function replace_callback( $matches ) {
    global $cache;
    $hash = sha1sum( $matches[0] );
    $cache[$hash] = $matches[0];
    return "REPLACE:$hash";
}

// replace all quotes with placeholders
$count = 0;
do {
    $input = preg_replace_callback( '~\[quoute.*?\[/quote\]~i', 'replace_callback', $input, -1, $count );
    // replace every occurence
} while ($count > 0);

function undo_replace( $matches ) {
    global $cache;
    return $cache[$matches[1]];
}

// restore the outer most quotes
$input = preg_replace_callback( '~REPLACE:[a-f0-9]{40}~i', 'undo_replace', $input );

// remove the references to the inner quotes
$input = preg_replace( '~REPLACE:[a-f0-9]{40}~i', '', $input );

, PHP . - , , , .

,
haggi

+2

preg_replace , . littel .

$position = strrpos($string, '[/quote:');  // this will get the position of last quote
$text = substr(strip_tags($string),$position+17); // this will get the data after the last quote used. 

, -.

0

Source: https://habr.com/ru/post/1761869/


All Articles