Jump to content

[SOLVED] Remove only line breaks between [code] [/code]


Garrett

Recommended Posts

This what I am doing and trying to achieve:

 

I have a text area on my website that allows users to input data into a mysql database. When a user tries to view a specific page it display the entered data to them using nl2br() to form the line breaks.

Now I am allowing use of bbcode and I have [ code] and [ /code] tags. This allows text to be entered unformatted which it surrounds with a div and pre tags. The problem is the nl2br() and pre combined are causing double new lines. So I need to be able to remove line breaks from only inside of [ code] [/code]

 

I have this regex:

/\[code\=(.*?)\](.*?)\[\/code\]/is

But I am unsure on how to edit it to remove line breaks. Any help would be great.

Link to comment
Share on other sites

Thanks for the help. It appears to be working perfectly if there is only one set of code tags. But if there are 2+ sets of code tags it merges the first [ code=] tag with the last [ /code] tag. In addition to this it doesn't remove the line breaks from the first [ code=] tag to the next

 tag.

Say I have this:
[code]
[ code=test]test
test
test[ /code]
[code= test2]test2
test2

test2
test2[ /code]

 

It will appear like this :

test

test

test[ code]

[ code=test2]
test2

test2
test2

It seems it is removing the / on the first end code tag.

 

Any help would be great.

 

Link to comment
Share on other sites

My approach:

 

<?php
$str = 'Text with a
linebreak
[code=test]test
test
test

test2
test2

test2
test2

';

$str = preg_replace_callback(

'~(\

[code[^\]]*\])(.*?)(\[/code\])~is',
create_function(
'$matches',
'return $matches[1] . str_replace(array("\r\n", "\r", "\n"), \'<br />\', $matches[2]) . $matches[3];'
),
$str
);
//run your bbcode function here, including nl2br()
echo $str;
?>[/code]

Link to comment
Share on other sites

@thebadbad, that solution would be along the lines I would have done :) Except I think you can simplify your pattern by eliminating all captures (of course, this assumes that there is no newlines / carriage returns within the code tags themselves  :shy:):

 

$str = preg_replace_callback(
'~\[code[^\]]*\].*?\[/code\]~is',
create_function(
	'$matches',
	'return str_replace(array("\r\n", "\r", "\n"), \'<br />\', $matches[0]);'
),
$str
);

 

A minor side note regarding escaped characters; some escape sequences can be avoided (of course, this is no big deal though):

 

~\[code[^]]*].*?\[/code]~

 

Note that inside a character class, the ] doesn't require escaping, as the regex engine treats that ] as a literal. The other character that doesn't require escaping is closing ] characters that are not part of character classes. Since there is no matching opening non escaped [ character, the regex engine will treat this too as a literal (can't do that the other way around though, as in [...\] as you'll get a Compilation failed: missing terminating ] for character class at offset ..x warning). Again though.. no big deal.. no harm in escaping things that don't actually require escaping.

 

EDIT Hmmm.. in the preview, that revised pattern (surrounded by nobbc tags) displays correctly... but not in the final post  :-\

 

Let's try that with some spacing?

~\[ code[^]]* ].*?\[ /code ]~

Link to comment
Share on other sites

Except I think you can simplify your pattern by eliminating all captures (of course, this assumes that there is no newlines / carriage returns within the code tags themselves  :shy:)

 

Yeah, should've thought of that :)

 

A minor side note regarding escaped characters; some escape sequences can be avoided (of course, this is no big deal though)

 

I know this, but I tend to always escape literal square brackets, to make it less confusing for less experienced regex'ers.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.