bloodgoat Posted April 21, 2009 Share Posted April 21, 2009 Working on the BBCode section of my shoutbox script. A few errors I can't seem to wrap my head around. Here are the few I have so far: <?php for($i=0;$i<count($post);$i++){ $post[$i][3] = str_replace(":as:", "<img src=\"http://i204.photobucket.com/albums/bb303/img0t/as.png\">", $post[$i][3]); $post[$i][3] = str_replace("[b]", "<b>", $post[$i][3]); $post[$i][3] = str_replace("[/b]", "</b>", $post[$i][3]); $post[$i][3] = str_replace("[i]", "<i>", $post[$i][3]); $post[$i][3] = str_replace("[/i]", "</i>", $post[$i][3]); } ?> Everything works fine. My trouble is, I'm trying to implement a BBCode URL tag. A bit like so: <?php $post[$i][3] = str_replace("[url=", "<a target=\"_blank\" href=\"", $post[$i][3]); $post[$i][3] = str_replace("]", "\">", $post[$i][3]); $post[$i][3] = str_replace("[/url], "</a>", $post[$i][3]); ?> The problem being, since "]" is set as it's own tag with it's own specific string replacement, it destroys the [b][/b] and [i][/i] tags. On output after putting URL tags in, where bold text WOULD go, it instead looks like: [b">I'm attempting to bold this statement[/b"> So, how can I circumvent this? I thought briefly that changing the order of the string replacements might alleviate this, but to no avail. So... any words of wisdom? Link to comment https://forums.phpfreaks.com/topic/155059-bbcode-help/ Share on other sites More sharing options...
soak Posted April 21, 2009 Share Posted April 21, 2009 You need a regex to do it all at once. Although it's not been touched in a while this is still good: http://pear.php.net/package/HTML_BBCodeParser If you've only got url's to do then this may work (untested): $content = preg_replace('!\])+\]([^\[])+\[/url\]!', '<a href="\\1">\\2</a>', $content); Link to comment https://forums.phpfreaks.com/topic/155059-bbcode-help/#findComment-815576 Share on other sites More sharing options...
C.Pearse Posted April 21, 2009 Share Posted April 21, 2009 How about this: <?php $post[$i][3] = preg_replace('#\[url=(.*?)\](.*?)\[/url\]#i', '<a href="$1">$2</a>', htmlentities($post[$i][3])); ?> Cheers Chris Link to comment https://forums.phpfreaks.com/topic/155059-bbcode-help/#findComment-815581 Share on other sites More sharing options...
soak Posted April 21, 2009 Share Posted April 21, 2009 $content = preg_replace('!\[url=(http://[^\]])+\]([^\[])+\[/url\]!', '<a href="\\1">\\2</a>', $content); Quote to put it in a code block and move the http:// :slap: Link to comment https://forums.phpfreaks.com/topic/155059-bbcode-help/#findComment-815588 Share on other sites More sharing options...
bloodgoat Posted April 21, 2009 Author Share Posted April 21, 2009 Bah, I wish my brain knew how to parse preg_replace strings. It all just looks like nonsense to me. Regardless... How about this: <?php $post[$i][3] = preg_replace('#\[url=(.*?)\](.*?)\[/url\]#i', '<a href="$1">$2</a>', htmlentities($post[$i][3])); ?> Cheers Chris This worked just fine, until I noticed it broke my test smiley. The output for that afterwards remains to be the normal <img src="url"> tag. Link to comment https://forums.phpfreaks.com/topic/155059-bbcode-help/#findComment-815601 Share on other sites More sharing options...
C.Pearse Posted April 21, 2009 Share Posted April 21, 2009 Probably because we used htmlentities(). Try using this: <?php function BBCode($post) { $post = htmlentities($post); $search = array( '#\[b\](.*?)\[/b\]#is', '#\[i\](.*?)\[/i\]#is', '#\[url=(.*?)\](.*?)\[/url\]#i', '#\:as\:#i' ); $replace = array( '<strong>$1</strong>', '<em>$1</em>', '<a href="$1" title="$2">$2</a>', '<img alt="" src="http://i204.photobucket.com/albums/bb303/img0t/as.png" />' ); $post = preg_replace($search, $replace, $post); $post = nl2br($post); return $post; } for($i = 0; $i < count($post); $i++) { $post[$i][3] = BBCode($post[$i][3]); } ?> Cheers Chris Link to comment https://forums.phpfreaks.com/topic/155059-bbcode-help/#findComment-815640 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.