Jump to content

please help about bbcode


teomanersan

Recommended Posts

hi guys..

i am using two bbcode tags for linking inside my site which are [bkz] [/bkz] and [bkz] [*bkz]

for this, i'm using this code..


[code]
$comment = preg_replace("#\[bkz](.*?)\[/bkz\]#ise", "'<a title=\"(bkz.\\1)\" href=\"ajaxcomsayfa.php?ara='.urlencode('\\1').'\">\\1</a>'",$comment);
$comment = preg_replace("#\[bkz](.*?)\[\*bkz\]#ise", "'<a title=\"(bkz.\\1)\" href=\"ajaxcomsayfa.php?ara='.urlencode('\\1').'\" >*</a>'",$comment);
[/code]

works fine.. but now i want to convert bbcode to html.. so i used preg_replace again..

[code]
$comment = preg_replace("#(<a title=(.+?) href=(.+?) >(.+?)</a>)#is","[bkz]\\1[/bkz]",$comment);
$comment = preg_replace("#(<a title=(.+?) href=(.+?) >\*</a>)#is","[bkz]\\1[/*bkz]",$comment);
[/code]

now the problem starts ... it gives the output like this IF i use $comment = striptags($comment) and both tags were used..

[bkz][bkz]test*[*bkz][/bkz]

if only one tag is used, nothing shows up :((

else it gives tags plus the html codes and drives me NUTS !!!!

please if anyone know how to get rid of this problem, let me know !!!

basicly what i want is:

<a title="(bkz.test)" href="ajaxcomsayfa.php?ara=test">test</a> TO -> [bkz]test[/bkz]

<a title="(bkz.test)" href="ajaxcomsayfa.php?ara=test">*</a> TO -> [bkz]test[*bkz]


thanx for your time and any help is HIGHLY appericiated..

good coding
Link to comment
https://forums.phpfreaks.com/topic/12610-please-help-about-bbcode/
Share on other sites

Hmm.. Are you trying to make bbcode ?
Try this function I'm using:
[code]<?php

function bbcode($text)
{
$pattern[] = '//';
$replace[] = '';

$pattern[] = '/\n/';
$replace[] = '<br>';

$pattern[] = '/\[b\](.*?)\[\/b\]/';
$replace[] = '<span style="font-weight:bold">$1</span>';

$pattern[] = '/\[i\](.*?)\[\/i\]/';
$replace[] = '<span style="font-style:italic">$1</span>';

$pattern[] = '/\[u\](.*?)\[\/u\]/';
$replace[] = '<span style="text-decoration:underline">$1</span>';

$pattern[] = '/\[color=(.*?)\](.*?)\[\/color\]/';
$replace[] = '<span style="color: $1">$2</span>';

$pattern[] = '/\[url=(.*?)\](.*?)\[\/url\]/';
$replace[] = '<a href="$1">$2</a>';

$pattern[] = '/\[url\](.*?)\[\/url\]/';
$replace[] = '<a href="$1">$1</a>';

$pattern[] = '/\[img\](.*?)\[\/img\]/';
$replace[] = '<img src="$1">';

$pattern[] = '/\[b\](.*?)\[\/b\]/';
$replace[] = '<b>$1</b>';

$pattern[] = '/\[bkz\](.*?)\[\/bkz\]/';
$replace[] = '<a title="(bkz.$1)" href="ajaxcomsayfa.php?ara=test">$1</a>';

$pattern[] = '/\[bkz\](.*?)\[\*bkz\]/';
$replace[] = '<a title="(bkz.$1)" href="ajaxcomsayfa.php?ara=test">$1</a>';

$text = preg_replace($pattern, $replace, $text);
return $text;
}

?>[/code]

P.S : I have added I have added what you want at the last 2 bbcode, save this file as functions.php or anything.

Usage:
[code]<?php
include('functions');
$text = "[bkz]test[/bkz]";
echo bbcode($text);
?>[/code]

Enjoy :)

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.