homepage Welcome to WebmasterWorld Guest from 54.166.105.24
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
Forum Library, Charter, Moderators: coopster & jatar k

PHP Server Side Scripting Forum

    
BBCode - Line Breaks and Paragraphs
Sp4rkyM4rk



 
Msg#: 4121366 posted 5:29 pm on Apr 24, 2010 (gmt 0)

I have recently coded out a BBCode system for parts of my website using preg_replace and function, and all of it works when it comes to creating tags which require input between them. However, since I created this BBCode file and included it in my page, it seems to override my former str_replace property I used to automatically convert line breaks to <br /> tags and paragraphs to </p><p> tags in the appropriate spots. I have removed this part of code now, since it doesn't function at all with the BBCode include.

So, I have inserted the similar code for paragraph conversion into my BBCode file and this is what I have:

<?php

function bbcode($input){
$input = strip_tags($input);
$input = htmlentities($input);

$search = array(
'/\r\n\r\n/is',
'/\[b\](.*?)\[\/b\]/is',
'/\[i\](.*?)\[\/i\]/is',
'/\[u\](.*?)\[\/u\]/is',
'/\[url=(.*?)\](.*?)\[\/url\]/is',
'/\[urlnw=(.*?)\](.*?)\[\/urlnw\]/is',
);

$replace = array(
'</p>

<p>',
'<span style="font-weight:bold">$1</span>',
'<span style="font-style:italic">$1</span>',
'<span style="text-decoration:underline">$1</span>',
'<a href="$1">$2</a>',
'<a href="$1" target="_blank">$2</a>',
);
}

?>


The following lines focus on the line to paragraph conversion:

'/\r\n\r\n/is',

'</p>

<p>',


I use the return between the paragraph tags to make the HTML markup tidy when viewing the source of the page.

That's all fine - works perfectly if I have a blank line in between text and the paragraphs are automatically put into place. However, when I try adding another code to represent single line breaks, the BBCode function is forced to place the <br /> tag in the right place, however the paragraph code kicks in and treats this tag as another blank line in addition to the new line I started using the 'return' key.

I can sort of understand why it's doing this, but I'm wondering if there is a solution and what it could be. My hunch is an if statement to tell the paragraph code not to execute when only one line break is present. I'm also wondering is str_replace could be used in the BBCode file as that was working perfectly before and treated new manually typed lines as a single <br /> tag and two as the paragraph tag.

If anybody could help with this I'd appreciate it a lot, thanks. :)

 

Readie

WebmasterWorld Senior Member 5+ Year Member



 
Msg#: 4121366 posted 6:14 pm on Apr 24, 2010 (gmt 0)

This is what I do:

$symbols = array(
'/[\r\n]{4,}/m' => '</p><p>',
'/[\r\n]{2,}/m' => '<br />'
);

foreach($symbols as $symb => $repl) {
$input = preg_replace($symb, $repl, $input);
}

May seem a bit wierd the loop/array etc, but hey, I just cut it out of my function that has ~100 items in that array :)

Sp4rkyM4rk



 
Msg#: 4121366 posted 6:56 pm on Apr 24, 2010 (gmt 0)

I added the search (/[\r\n]{4,}/is and /[\r\n]{2,}/is) and replacements (</p><p> and <br />) to my code and it works wonders so thanks a lot for that. :)

Although (and call me fussy, hehe), is it possible to separate the paragraphs in the source code? So going from this:

<p>This is paragraph 1.</p><p>This is another one.</p><p>We are all cramped together</p>

to this:

<p>This is paragraph 1.</p>

<p>This is another one.</p>

<p>We are free.</p>


Since I was editing the replacement tags, the other part of the code considers the blank space between these paragraph tags as another line break so puts more spaces between the text. Is it possible to say:

  • If there is only one line break, add <br />.
  • If there are two line breaks, add </p> to the end and <p> on the next line where text is found.
  • If there are three or more line breaks, put the paragraph tags in appropriate spots and <br /> tags between them (like valid HTML).


If this isn't possible I wouldn't mind using the automatic paragraph tags after two line breaks, and insert <br /> tags myself, but the HTML tags don't work since I'm replacing them with BBCode. Taking out strip_tags causes the HTML markup to show in the article.

Also, is there a way to add BBCode which only requires one tag? For example, like in this forum use [ hr ] instead of [ hr ][/hr]. And for an easy method of enabling custom line breaks, add [ br ] instead of [ br ][/br] (ommitting the spaces). The latter does work but I'd rather type a single tag or I might as well use HTML markup.

Readie

WebmasterWorld Senior Member 5+ Year Member



 
Msg#: 4121366 posted 7:32 pm on Apr 24, 2010 (gmt 0)

I'll tell you what, I'll send you a link to a .phps that I've shared with a few people before, it's the function I use for parsing textarea/input-text, with the optional allowance for HTML and BB code aswell.

Check your stickymail inbox.

FourDegreez

WebmasterWorld Senior Member 10+ Year Member



 
Msg#: 4121366 posted 1:04 am on Apr 25, 2010 (gmt 0)

Readie, you can call preg_replace just once, passing an array of patterns and a corresponding array of replacements.

Readie

WebmasterWorld Senior Member 5+ Year Member



 
Msg#: 4121366 posted 11:19 am on Apr 25, 2010 (gmt 0)

I know four - however I call my function on the cron job/a new news post/edit, and a new comment and cache the formatted output on the server and just serve that to visitors, so the higher CPU usage brought by foreach() isn't really an issue.

Having it as '/search/' => 'replace', just makes it neater, and easier to maintain.

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved