/    Sign up×
Community /Pin to ProfileBookmark

Wrapped/nested bbcodes regexp

I have a problem with my current bbcode system.
When I nest a code with the same code, it won’t work.

Example:
{quote} {quote} Bla bla bla {/quote} bla bla {/quote}

This will turn out like this:

[quote]

{quote}Bla bla bla

[/quote]

{/quote}

The function I use

[code=php]
function tags_format ($str) {

// Search for tags
$search = array(
‘/[quote](.*?)[/quote]/is’,
‘/[quote=(.*?)](.*?)[/quote]/is’
);

// Replace the tags
$replace = array(
‘<blockquote><h4>Quote:</h4>$1</blockquote>’,
‘<blockquote><h4>$1 wrote:</h4>$2</blockquote>’
);

// Return the string with the tags
$str = preg_replace ($search, $replace, $str);

return $str;
}

[/code]

Any help is greatly appriciated.

to post a comment
PHP

3 Comments(s)

Copy linkTweet thisAlerts:
@NogDogApr 30.2009 — This is where the regexp approach to bbcode starts to break down. It might be possible to do something with [url=http://www.php.net/manual/en/regexp.reference.php#regexp.reference.recursive]recursive patterns[/url], but my brain always starts to explode when I try to figure out how to use that for something like bbcode. My best suggestion would be to look at using either the [url=http://php.net/bbcode]bbcode extension[/url] or the [url=http://pear.php.net/package/HTML_BBCodeParser]PEAR HTML_BBCodeParser package[/url].
Copy linkTweet thisAlerts:
@QuidamauthorApr 30.2009 — That's what I was afraid of..

The PEAR BBCodeParser is so difficult to customize and the php bbcode extension fails more than regexp.

But I guess that people will have to get along with single-quotes :p
Copy linkTweet thisAlerts:
@NogDogApr 30.2009 — Sorry to hear that about those extensions/packages: I've not tried using them myself. Maybe someone knows of a 3rd-party class/package that works better?
×

Success!

Help @Quidam spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.18,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...