FRIHOSTFORUMSSEARCHFAQTOSBLOGSDIRECTORY
You are invited to Log in or Register a Frihost Account!

How to realize "BB-Code"

 


shinji_x19
hi,

in this tutorial I explain how to realize "bb-codes".
As examples I use the bb-codes text and http://php.de

You can parse the bb-code text with preg_replace():

php:
$text = ("a [b]bold[/b] text!");
$text = preg_replace("|\[b\](.*)\[/b\]|Uism","$1",$text);


Explanation to the pattern-Part of preg_replace:
[ and ] must be masceraded with a backslash \.
(.*) is a substitute symbol for an undefined text (like the % by MySQL-queries).

The pattern-modifiers Uism:
U: This modifier inverts the "greediness" of the quantifiers so that they are not greedy by default, but become greedy if followed by "?". It is not compatible with Perl. It can also be set by a (?U) modifier setting within the pattern.
i: If this modifier is set, letters in the pattern match both UPPER and lower case letters.
s: If this modifier is set, a dot metacharacter in the pattern matches all characters, including newlines. Without it, newlines are excluded. This modifier is equivalent to Perl's /s modifier. A negative class such as [^a] always matches a newline character, independent of the setting of this modifier.
m: When this modifier is set, the "start of line" and "end of line" constructs match immediately following or immediately before any newline in the subject string, respectively, as well as at the very start and end. This is equivalent to Perl's /m modifier. If there are no "\n" characters in a subject string, or no occurrences of ^ or $ in a pattern, setting this modifier has no effect.

It becomes somewhat more difficult if you want to parse [URL]-bb-codes and the url should be shortened if it contains more than 30 symbols.
Here we need preg_replace_callback(), create_function(), strlen() and substr().

preg_replace_callback() is needed because we want to change the url for the title,
the function is similarly structured to the already known preg_replace().
php:
$text = preg_replace_callback("||Uism","",$text);

The pattern-modifiers are already known from the 1st example.
The new thing here is that we need at the second position a function.
We'll use create_function() for that.
php:
$text = preg_replace_callback("||Uism",create_function('$matches',''),$text);

$matches is an array,
[0] is the whole string of the callback,
starting from [1] there will be the text in the brackets.
In this case is thus [1] http:// and [2] the unknown text (.*).

We check with strlen() if the string contains more than 30 symbols.
If that's true we'll return with substr($matches[2],0,17) the first 17 symbols of the url and and then with substr($matches[2],-5) the last 5 symbols.

php:
create_function(
'$matches',
'if (strlen($matches[2]) > 30) {
return "<a href="http://".$matches[2]."" target=_blank>http://".substr($matches[2],0,17)."[...]".substr($matches[2],-5)."</a>";
}
')


If the string contains less than 30 symbols we'll return the url uncutted.

Here's the whole code of this example:

php:
$text = ("The php.de tutorials - section: http://php.de/viewforum.php?f=16&justafewsymbols=thatwecanbesuretheurlcontainsmorethan30symbols");

$text = preg_replace_callback("|\[url\](http://)(.*)\[/url\]|Uism",create_function(
'$matches',
'if (strlen($matches[2]) > 30) {
return "<a href="http://".$matches[2]."" target=_blank>http://".substr($matches[2],0,17)."[...]".substr($matches[2],-5)."</a>";
} else {
return "<a href="http://".$matches[2]."" target=_blank>http://".$matches[2]."</a>";
}
'),$text);


If you want to use the [php]-bb-code the function highlight_string() could be helpfully for you,
you can use highlight_string() in combination with preg_replace_callback()..
MrBlueSky
Readable version: http://forums.devnetwork.net/viewtopic.php?t=24220
Animal
Moved to Tutorials forum
Related topics

BB Codes
BB Colours
BB Codes
BB Code :|
uncoded code ! :P

BB Code [EDIT: Offering 150 FRIH$]
Smilies question, PhP forum.
[RESOLVED] How to add image or text to link in signature?
Help with PhpBB
Guissmo's Javascript (1)

a game online
PHP - BBcode Parsing
Tutorial System
UBB Code / Smiley Parsing
Need help to display Yahoo Online Status Indicator on site
Reply to topic    Frihost Forum Index -> Miscellaneous -> Tutorials

FRIHOST HOME | FAQ | TOS | ABOUT US | CONTACT US | SITE MAP
© 2005-2007 Frihost, forums powered by phpBB.