0

Disclaimer! I have no prior knowledge of php, I just need a clear answer.

I have the string $content, and I want to replace any [ or ] between the [nobb] and [/nobb] tags inside it with ^ and *. And I need also to do this again but this time to remove the [nobb] and [/nobb] tags.

As you may have guessed, I want to do this for bbcode exclusion :D.

Thanks, sincerely, n.utiu.

2
Contributors
9
Replies
10
Views
6 Years
Discussion Span
Last Post by n.utiu
1

Not sure if it can be done in one go, but try this:

$result = preg_replace('%(\[nobb\].*?)(\[)(.*?\[/nobb\])%m', '$1^$3', $content);
$result = preg_replace('%(\[nobb\].*?)(])(.*?\[/nobb\])%m', '$1*$3', $result);

For the other just use str_replace().

Edited by pritaeas: n/a

0

Not sure if it can be done in one go, but try this:

$result = preg_replace('%(\[nobb\].*?)(\[)(.*?\[/nobb\])%m', '$1^$3', $content);
$result = preg_replace('%(\[nobb\].*?)(])(.*?\[/nobb\])%m', '$1*$3', $result);

For the other just use str_replace().

That replaced just the first occurrence of [ or ], is there any way to replace them all ?

1

Can you give an example, because I cannot reproduce your problem. Did you try without the m modifier ?

Edited by pritaeas: n/a

0

Hmz, apparantly not recursive... It may be possible with preg_replace_callback. I'll get back to you.

Btw, does it matter if other loose brackets are also replaced ?

Edited by pritaeas: n/a

0

Hmz, apparantly not recursive... It may be possible with preg_replace_callback. I'll get back to you.

Btw, does it matter if other loose brackets are also replaced ?

Thanks, I appreciate.

0

Hmz, apparantly not recursive... It may be possible with preg_replace_callback. I'll get back to you.

Btw, does it matter if other loose brackets are also replaced ?

no

0

If I understand correctly, this is what you want to do:

function regex_match($match) {
  return str_replace(array('[', ']'), array('^', '*'), $match[1]);
}

$content = '[nobb]This is my [b]test[/b] string[/nobb]';
$parsed_content = preg_replace_callback('%\[nobb\](.*?)\[/nobb\]%', regex_match, $content);
echo $parsed_content;

Edited by pritaeas: n/a

0

If I understand correctly, this is what you want to do:

function regex_match($match) {
  return str_replace(array('[', ']'), array('^', '*'), $match[1]);
}

$content = '[nobb]This is my [b]test[/b] string[/nobb]';
$parsed_content = preg_replace_callback('%\[nobb\](.*?)\[/nobb\]%', regex_match, $content);
echo $parsed_content;

Exactly! Kudos!

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.