|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * MIT License |
| 4 | + * |
| 5 | + * Copyright (c) 2017 Igor M Osipov |
| 6 | + * |
| 7 | + * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 8 | + * of this software and associated documentation files (the "Software"), to deal |
| 9 | + * in the Software without restriction, including without limitation the rights |
| 10 | + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 11 | + * copies of the Software, and to permit persons to whom the Software is |
| 12 | + * furnished to do so, subject to the following conditions: |
| 13 | + * |
| 14 | + * The above copyright notice and this permission notice shall be included in all |
| 15 | + * copies or substantial portions of the Software. |
| 16 | + * |
| 17 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 18 | + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 19 | + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 20 | + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 21 | + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 22 | + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 23 | + * SOFTWARE. |
| 24 | + */ |
| 25 | + |
| 26 | +namespace Igorakaamigo\Utils; |
| 27 | + |
| 28 | +/** |
| 29 | + * Class BBCode |
| 30 | + * |
| 31 | + * A lightweight BBCode subset implementation. |
| 32 | + * |
| 33 | + * @package Igorakaamigo\Utils |
| 34 | + */ |
| 35 | +class BBCode |
| 36 | +{ |
| 37 | + private static $_patterns = array( |
| 38 | + '#\[b\](.*?)\[/b\]#si' => '<strong>\1</strong>', |
| 39 | + '#\[i\](.*?)\[/i\]#si' => '<em>\1</em>', |
| 40 | + '#\[u\](.*?)\[/u\]#si' => '<span style="text-decoration: underline;">\1</span>', |
| 41 | + '#\[s\](.*?)\[/s\]#si' => '<del>\1</del>', |
| 42 | + '#\[url\](.*?)\[/url\]#si' => '<a href="\1">\1</a>', |
| 43 | + '#\[url=(.*?)\](.*?)\[/url\]#si' => '<a href="\1">\2</a>', |
| 44 | + '#\[img\](.*?)\[/img\]#si' => '<img src="\1" alt="" />', |
| 45 | + '#\[quote(=".*?")?\](.*?)\[/quote\]#si' => '<blockquote>\2</blockquote>', |
| 46 | + '#\[code\](.*?)\[/code\]#si' => '<code style="white-space: pre;">\1</code>', |
| 47 | + '#\[size=(\d+)\](.*?)\[/size\]#si' => '<span style="font-size: \1px;">\2</span>', |
| 48 | + '#\[size="(.*?)"\](.*?)\[/size\]#si' => '<span style="font-size: \1;">\2</span>', |
| 49 | + '#\[color="(.*?)"\](.*?)\[/color\]#si' => '<span style="color: \1;">\2</span>', |
| 50 | + '#\[li\](.*?)\[/li\]#si' => '<li>\1</li>', |
| 51 | + '#\[ul\](.*?)\[/ul\]#si' => '<ul>\1</ul>', |
| 52 | + '#\[ol\](.*?)\[/ol\]#si' => '<ol>\1</ol>', |
| 53 | + '#\[table\](.*?)\[/table\]#si' => '<table>\1</table>', |
| 54 | + '#\[tr\](.*?)\[/tr\]#si' => '<tr>\1</tr>', |
| 55 | + '#\[td\](.*?)\[/td\]#si' => '<td>\1</td>', |
| 56 | + '#(^\s+)|(\s+$)#si' => '', |
| 57 | + ); |
| 58 | + |
| 59 | + /** |
| 60 | + * BBCode translation |
| 61 | + * |
| 62 | + * @param string $sourceString A string containing BBCode tags |
| 63 | + * @return string HTML string |
| 64 | + */ |
| 65 | + static function convert($sourceString) |
| 66 | + { |
| 67 | + return preg_replace( |
| 68 | + array_keys(self::$_patterns), |
| 69 | + array_values(self::$_patterns), |
| 70 | + htmlentities($sourceString) |
| 71 | + ); |
| 72 | + } |
| 73 | +} |
0 commit comments