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.

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().

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 ?

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

http://en.wikipedia.org/wiki/Localhost

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

[nobb]test[/nobb] should produce [nobb]^b*test^/b*[/nobb]

but I only get [nobb]^b*test[/b][/nobb]

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 ?

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.

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

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;

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!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.