Jump to content

bbcode_create() + nl2br() + <img />

Man In Tan

Recommended Posts

I can't figure out how to use the bbcode_create() function (or any other function) to create valid tags that do not have an end tag (such as <img /> ).The example given by the official website has a problem when combined with the nl2br() function.

'img'=>	  array('type'=>BBCODE_TYPE_NOARG,					'open_tag'=>'<img src="', 'close_tag'=>'" alt="posted image" />',					'childs'=>'')

Someone could easily break my XHTML by typing


I know a solution exists, but I can't seem to find it. This W3 Schools forum has a solution implemented:





How do I fix my tags? Edited by Man In Tan
Link to comment
Share on other sites

Good idea, but it didn't work. the line breaks got carried over and converted.

$text = bbcode_parse($BBHandler,$text);$text = nl2br($text);echo $text;

$text = nl2br($text);$text = bbcode_parse($BBHandler,$text);echo $text;

Both result in:

<img src="<br />xhtml_breaker.png<br />" alt="posted image" />

Link to comment
Share on other sites

Perhaps if you define the whole line replacement thing as something happening during the parsing... like:

function bbcode_primary_content_handler($content, $argument) {	return nl2br($content, true);}$bbcode = array('' => array(	'type' => BBCODE_TYPE_ROOT,	'content_handling' => 'bbcode_primary_content_handler'),'img' => array(	'type'=>BBCODE_TYPE_NOARG,	'open_tag' => '<img src="', 'close_tag' => '" alt="posted image" />',	'childs' => '')'i' => array(	'type' => BBCODE_TYPE_NOARG,	'open_tag' => '<i>', 'close_tag'=>'</i>',	'content_handling' => 'bbcode_primary_content_handler')//etc.);

That way, you simply exclude any unknown content of being parsed in a special way.

Link to comment
Share on other sites

I didn't understand what the handlers did until your post, so I assumed they were for some advanced feature I didn't need yet.I seems that the content handlers get stacked, so by setting "'content_handling' => 'bbcode_primary_content_handler'" to the root and 'i', any italic text will be double spaced. I tried overriding the 'img' content handler, but it just stacked it as well. So, I added a content handler to strip new lines from any operating system. Now there are no line brakes to be converted to "<br />".

function fff_bbcode_atribute($data, $not_implemented) {$pattern = array("'\n'", "'\r'");$data = preg_replace($pattern, '', $data);return $data;}

I was still having issues of a different kind with an "a" tag, so I rewrote it to experiment.

'a' => array('type' => BBCODE_TYPE_ARG, 'open_tag' => '<a href="{PARAM}{CONTENT}{TEST}">', 'close_tag' => '</a>', 'param_handling' => 'fff_bbcode_atribute')



<a href="contentcontent{TEST}">content</a>

The parameter gets overwritten by the content.I flipped the variables, and now it works fine.

function fff_bbcode_atribute($not_implemented, $data) {$pattern = array("'\n'", "'\r'");$data = preg_replace($pattern, '', $data);return $data;}

<a href="paramcontent{TEST}">content</a>

As a side note, the 'parent' string seems to get ignored completely. If a tag should only be used inside certain other tags, it must be specifically excluded from all tags that it should not be included it through the 'parent' stings of those tags.I'm still having issues with the BBCODE_TYPE_SINGLE BB code type. It's not giving me any trouble for now, but I was wondering if it could cause troubles with BB code nesting.

'hr/' => array('type' => BBCODE_TYPE_SINGLE, 'open_tag' => '<hr />', 'close_tag' => '[test tag]')

[blockquote][a=/]Index[/a][hr/][i]Visit our home page![/i][/blockquote]


<blockquote><br /><a href="/">Index</a><br /><hr /><br /><i>Visit our home page!</i><br />[test tag]</blockquote>

It's seems that even with the BBCODE_TYPE_SINGLE tag type, it looks for an end tag. And when it doesn't find one, it adds one to preserve tag nesting. It's easy enough to fix by using 'close_tag' => '', but it shows me that the tag does't really end until it finds (or adds) a [/hr/] tag. Could this cause an error in nesting?

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

  • Create New...