Garrett Posted October 18, 2009 Share Posted October 18, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/178096-solved-remove-only-line-breaks-between-code-code/ Share on other sites More sharing options...
Daniel0 Posted October 18, 2009 Share Posted October 18, 2009 You could use preg_split. $string = <<<EOF foo bar [code]test foo baz EOF; $newString = ''; foreach (preg_split('#\[/?code\]#', $string) as $i => $part) { if ($i % 2 == 0) { $newString .= nl2br($part); } else { $newString .= ' ' . $part . ' '; } } echo $newString;[/code] Quote Link to comment https://forums.phpfreaks.com/topic/178096-solved-remove-only-line-breaks-between-code-code/#findComment-939270 Share on other sites More sharing options...
Garrett Posted October 18, 2009 Author Share Posted October 18, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/178096-solved-remove-only-line-breaks-between-code-code/#findComment-939349 Share on other sites More sharing options...
thebadbad Posted October 18, 2009 Share Posted October 18, 2009 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] Quote Link to comment https://forums.phpfreaks.com/topic/178096-solved-remove-only-line-breaks-between-code-code/#findComment-939370 Share on other sites More sharing options...
Garrett Posted October 18, 2009 Author Share Posted October 18, 2009 Seems to work wonderfully Thanks. Quote Link to comment https://forums.phpfreaks.com/topic/178096-solved-remove-only-line-breaks-between-code-code/#findComment-939378 Share on other sites More sharing options...
nrg_alpha Posted October 19, 2009 Share Posted October 19, 2009 @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 ): $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 ]~ Quote Link to comment https://forums.phpfreaks.com/topic/178096-solved-remove-only-line-breaks-between-code-code/#findComment-939732 Share on other sites More sharing options...
thebadbad Posted October 19, 2009 Share Posted October 19, 2009 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 ) 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. Quote Link to comment https://forums.phpfreaks.com/topic/178096-solved-remove-only-line-breaks-between-code-code/#findComment-939854 Share on other sites More sharing options...
nrg_alpha Posted October 19, 2009 Share Posted October 19, 2009 I know this, but I tend to always escape literal square brackets, to make it less confusing for less experienced regex'ers. Ah ok.. I knew that you knew that I knew that you knew that.. was just testing. Quote Link to comment https://forums.phpfreaks.com/topic/178096-solved-remove-only-line-breaks-between-code-code/#findComment-939876 Share on other sites More sharing options...
thebadbad Posted October 19, 2009 Share Posted October 19, 2009 Ah ok.. I knew that you knew that I knew that you knew that.. was just testing. Sure It's always good to point things out, even if they're slightly off topic. People might actually learn something! Quote Link to comment https://forums.phpfreaks.com/topic/178096-solved-remove-only-line-breaks-between-code-code/#findComment-939883 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.