Jump to content

Trying to change '['


php_joe

Recommended Posts

I'm editing a bbs program I have and I'm having problems changing the BBCode into HTML.

 

I tried:

$message = str_replace('[b]', '<b>', $message);

which didn't work.

 

I tried:

$message = str_replace('[', '<', $message);

which also didn't work.

 

Why can't I change '['?

Link to comment
Share on other sites

Actually, I'm trying to change [b] into <b>

 

But, for some reason, when I tried

$clean_message = str_replace('[b]', '<b>', $message);

by itself it works, but when I put

$clean_message = str_replace('[b]', '<b>', $message);
$clean_message = str_replace('[/b]', '</b>', $message);

In, only the second one works. :(

Link to comment
Share on other sites

Try giving your variable different names? And don't you mean to put $clean_message in the second one? If you don't you will get two strings, one stripping the and leaving the and vise versa.

 

$clean = str_replace('[b]', '<b>', $message);
$clean_message = str_replace('[/b]', '</b>', $clean);

 

Try that. I am just kinda playing around with it...if this doesn't work I will actually go into my FTP and try working with it.

 

Did you try the preg_replace() function I suggested in my last post?

Link to comment
Share on other sites

Use arrays instead of multiple instances of str_replace, example:

 

<?php

$msg = "Hello [b]world[/b]! [u][i]Ohhhh[/i][/u]!";

$bbcode = array( '[b]', '[/b]',
                 '[i]', '[/i]',
                 '[u]', '[/u]' );

$html = array( '<b>', '</b>',
               '<i>', '</i>',
               '<u>', '</u>' );

$msg = str_replace($bbcode, $html, $msg);

echo $msg;

?>

 

However whne doing bbcodes I prefer to use regex. regex is more powerful allowing you to do more advanced bbcodes

Link to comment
Share on other sites

I would definitely go with wildteen88 advice if you have multiple tags you are trying to do it with.

if you have multiple tags, just add single str_replaces, and:

I think you have it backwards

 

Change this:

$message = str_replace('[b]', '<b>', $message);

 

To

$message = str_replace('<b>', '[b]', $message);

 

<a href="http://us2.php.net/str_replace">str_replace</a>

 

EDIT: If that doesn't work, try this.

 

$message = preg_replace('[b]','<b>', $message);

This:

$message = str_replace('[b]', '<b>', $message);

is actually correct.

Ted

Link to comment
Share on other sites

yeah, array does work here, however, special tags such as link would require the work done separately.

Ted

That's why I mentioned to do regex with advanced tags such as link-title str_replace is just too basic. You can do it on swell swop with regex. However reqex does take time to get used to. It tool me a few months to get used to it.

 

Heres a code snippet for bbcode parser with regex to get you started quickly - CLICKY

Link to comment
Share on other sites

yeah, array does work here, however, special tags such as link would require the work done separately.

Ted

That's why I mentioned to do regex with advanced tags such as link-title str_replace is just too basic. You can do it on swell swop with regex. However reqex does take time to get used to. It tool me a few months to get used to it.

 

Heres a code snippet for bbcode parser with regex to get you started quickly - CLICKY

wildteen, does this work? because I am having some trouble with bb codes, maybe i can use this.

Ted

Link to comment
Share on other sites

To get it to work you just call the function bbcode_format and pass the string with your bbcodes in as the first parameter, example:
[code=php:0]<?php

// place bbcode parser code snippet here

$msg = "[b]Hello world![/b] Umm [u]BBCODES[/u]

[url=http://www.google.com]google.com[/url] <-- Ohh! a link";

$msg = bbcode_format($msg);

echo nl2br($msg);

?>

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.