I've been working on this for awhile now, and this is what I came up with to parse some BB-like code in my text items:
PHP Code:
$item['text'] = preg_replace('/\[abbr="(.+)"\](.+)\[\/abbr\]/', '<abbr title="\1">\2</abbr>', $item['text']);
$item['text'] = preg_replace('/\[img="(.+)"\](.*)\[\/img\]/', '<img src="\1" alt="\2" />', $item['text']);
$item['text'] = preg_replace('/\[img="(.+)"\]/', '<img src="\1" alt="" />', $item['text']);
$item['text'] = preg_replace('/\[link="(.+)"\](.+)\[\/link\]/', '<a href="\1">\2</a>', $item['text']);
$item['text'] = preg_replace('/\[link="(.+)"\]/', '<a href="\1">\1</a>', $item['text']);
$item['text'] = preg_replace('/\[(left|right)\](.+)\[\/\1\]/', '<p class="\1">\2</p>', $item['text']);
//this should fix the <p>s around our text, but remove on [left] and [right] tags
$item['text'] = '<p>'.preg_replace("/(\r|\n)+/", "</p>\r\n<p>", $item['text']).'</p>';
$item['text'] = str_replace('<p><p', '<p', $item['text']);
$item['text'] = str_replace('</p></p>', '</p>', $item['text']);
The last couple of str_replaces to remove those extra tags seem rather clumsy, and I'd appreciate your opinion. Also, this is an extraordinary amount of regular expression overhead. Perhaps I should store the parsed code in the database after all...
Bookmarks