Jump to content

Trying to change '['


php_joe

Recommended Posts

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);

Link to comment
https://forums.phpfreaks.com/topic/40995-trying-to-change/#findComment-198520
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
https://forums.phpfreaks.com/topic/40995-trying-to-change/#findComment-198528
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
https://forums.phpfreaks.com/topic/40995-trying-to-change/#findComment-198532
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
https://forums.phpfreaks.com/topic/40995-trying-to-change/#findComment-198533
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
https://forums.phpfreaks.com/topic/40995-trying-to-change/#findComment-198572
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
https://forums.phpfreaks.com/topic/40995-trying-to-change/#findComment-198630
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
https://forums.phpfreaks.com/topic/40995-trying-to-change/#findComment-198642
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
https://forums.phpfreaks.com/topic/40995-trying-to-change/#findComment-198791
Share on other sites

Archived

This topic is now archived and is closed to further replies.

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