3 Replies - 6969 Views - Last Post: 15 October 2008 - 03:05 PM Rate Topic: -----

#1 animedude123  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 80
  • Joined: 03-August 07

Format Plain Text, Remove BB Code

Posted 14 October 2008 - 09:19 PM

Hey,

Right now I'm pulling my vbulletin posts from the database, and its "formatted" but in plain text. When it gets echo'd in php though, it gets all scrunched together because it doesn't have any <br>'s in it. It also has BB code in it ([ code ] for example).

So is there a way I can take

blah blah
[bb]blah[/bb]
blah blah


and change it into:

blah blah<br>
blah<br>
blah blah<br>


Right now the pagetext is attached to a variable then just echo'd out. I'm really not sure how to do this. Would I need to search the text for BB Code? And then for adding in <br>'s...I have no idea. Or is there another way? I mean, vbulletin does it :P I'm just not sure how.

Thanks in advance for any help!

Is This A Good Question/Topic? 0
  • +

Replies To: Format Plain Text, Remove BB Code

#2 Martyr2  Icon User is offline

  • Programming Theoretician
  • member icon

Reputation: 5078
  • View blog
  • Posts: 13,705
  • Joined: 18-April 07

Re: Format Plain Text, Remove BB Code

Posted 14 October 2008 - 09:45 PM

Look into the PHP function preg_replace, it is going to be a great friend to you for this. You are going to be able to match patterns like BB code tags and replace them with whatever you like. You may have limited luck with a simple str_replace as well, but preg_replace is going to be the gem in this situation.

:)
Was This Post Helpful? 0
  • +
  • -

#3 LoveSquid  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 47
  • Joined: 14-October 08

Re: Format Plain Text, Remove BB Code

Posted 15 October 2008 - 08:55 AM

I think you can also take of some of it by using nl2br when displying. Here is the link:nl2br PHP Manual

$example = nl2br($row['example']);


Was This Post Helpful? 0
  • +
  • -

#4 animedude123  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 80
  • Joined: 03-August 07

Re: Format Plain Text, Remove BB Code

Posted 15 October 2008 - 03:05 PM

Cool, but what about adding <br>'s? What would I search for to find breaks in lines? \n?

EDIT: Just saw LoveSquid's post, thanks! I'll try a mix of those 2 suggestions.

EDIT 2: Ok, so I just tried it and ran into a problem.

Here is an example of the bb code I'm trying to filter out: [ * ] (Without spaces)

So the replace looks like this: $patterns[2] = '/[*]/';

The problem though is it is only replacing the *. Anyone able to help me with replacing the whole [ * ] with something else? (<li> is my goal)

EDIT 3: Ok, fixed! I used str_replace instead :D Now everything seems to be working perfectly! Thanks for the help.

This post has been edited by animedude123: 15 October 2008 - 08:21 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1