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.

Recommended Answers

Not sure if it can be done in one go, but try this:
[code]
$result = preg_replace('%([nobb].?)([)(.?[/nobb])%m', '$1^$3', $content);
$result = preg_replace('%([nobb].?)(])(.?[/nobb])%m', '$1*$3', $result);
[/code]

For the other just use str_replace().

Jump to Post

Can you give an example, because I cannot reproduce your problem. Did you try without the m [url=http://php.net/manual/en/reference.pcre.pattern.modifiers.php]modifier[/url] ?

Jump to Post

All 9 Replies

Re: preg_replace question 80 80

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

Re: preg_replace question 80 80

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 ?

Re: preg_replace question 80 80

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

Re: preg_replace question 80 80

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]

Re: preg_replace question 80 80

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 ?

Re: preg_replace question 80 80

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.

Re: preg_replace question 80 80

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

Re: preg_replace question 80 80

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;
Re: preg_replace question 80 80

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 1.19 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.