codeconsortium/ccdn-component-bb-code
CCDNComponent BBCode Library for Laravel/PHP projects. Provides a BBCode parsing component to convert forum-style tags into HTML, intended for integrating rich text formatting in applications.
Installation Add the package via Composer:
composer require codeconsortium/ccdn-component-bb-code
Require the autoloader in your Laravel project (if not auto-loaded via Composer).
Basic Setup Initialize the BBCode parser in a Laravel service provider or controller:
use CCDN\Component\BBCode\BBCode;
$bbcode = new BBCode();
First Use Case: Parsing BBCode Parse a simple BBCode string into HTML:
$input = "[b]Hello[/b], [i]World![/i]";
$output = $bbcode->parse($input);
// Output: <strong>Hello</strong>, <em>World!</em>
Registering Custom Tags (Optional)
Define custom BBCode tags (e.g., [code]):
$bbcode->addTag('code', function ($text) {
return '<pre><code>' . htmlspecialchars($text) . '</pre></code>';
});
Sanitizing User Input Use BBCode parsing to sanitize and convert user-generated content (e.g., forum posts, comments) into safe HTML:
$userInput = request()->input('content');
$safeHtml = $bbcode->parse($userInput);
Integration with Laravel Blade Create a Blade directive or helper to parse BBCode in views:
// In a service provider:
Blade::directive('bbcode', function ($expression) {
return "<?php echo app('bbcode')->parse({$expression}); ?>";
});
Usage in Blade:
@bbcode($post->content)
Storing Parsed Output Cache parsed BBCode output to avoid reprocessing (e.g., for static pages or frequent reads):
$cacheKey = 'bbcode_' . md5($input);
$parsed = Cache::remember($cacheKey, now()->addHours(1), function () use ($bbcode, $input) {
return $bbcode->parse($input);
});
Validation Before Parsing
Validate BBCode input to restrict allowed tags/attributes (e.g., block [img] tags with src validation):
if (strpos($input, '[img') !== false) {
$input = preg_replace('/\[img\b[^\]]*\](.*?)\[\/img\]/i', '[img]$1[/img]', $input);
}
saved):
$post->saved(function ($post) {
$post->content_html = app('bbcode')->parse($post->content);
$post->save();
});
return response()->json([
'content' => $bbcode->parse($request->content),
]);
public function handle($request, Closure $next) {
$request->merge(['safe_content' => $bbcode->parse($request->content)]);
return $next($request);
}
XSS Vulnerabilities
[script]) can execute JavaScript.$bbcode->disableTags(['script', 'iframe']);
Purifier for additional sanitization.Performance with Large Inputs
Nested Tags
[b][i]text[/b][/i]) may break parsing.if (preg_match('/\[(\w+)\].*\[\/\1\]/i', $input) === 0) {
throw new \InvalidArgumentException('Invalid BBCode nesting.');
}
PHP 5.3 Legacy
$bbcode->setDebug(true);
$output = $bbcode->parse($input); // Check logs for errors.
$bbcode->setUnsupportedTagCallback(function ($tag) {
Log::warning("Unsupported BBCode tag: {$tag}");
});
Custom Tag Handlers Extend functionality by adding custom tags:
$bbcode->addTag('highlight', function ($text) {
return '<mark>' . $text . '</mark>';
});
Attribute Handling
Parse attributes in BBCode tags (e.g., [url=https://example.com]Link[/url]):
$bbcode->addTag('url', function ($text, $attributes) {
$url = $attributes['href'] ?? '#';
return "<a href=\"{$url}\">{$text}</a>";
});
Pre/Post Processing Hook into parsing lifecycle:
$bbcode->setPreProcessCallback(function ($input) {
return str_replace('[quote]', '<blockquote>', $input);
});
$bbcode->setPostProcessCallback(function ($output) {
return str_replace('</blockquote>', '[/quote]', $output);
});
Laravel Service Container Bind the BBCode parser as a singleton for dependency injection:
$this->app->singleton('bbcode', function () {
$bbcode = new BBCode();
$bbcode->disableTags(['script']);
return $bbcode;
});
Usage:
$parsed = app('bbcode')->parse($input);
How can I help you explore Laravel packages today?