Jump to content

bbcode code


kimla

Recommended Posts

Hi again.

 

static function code($string)
{
    return preg_replace("/\[code\](.*?)\[\/code\]/", "<div id='code'>$1</div>", $string);
}

 

When i type:

[code]
<br />
<?php
echo 'Hello world';
?>
<br />

[/code]

 

It doesnt recognize the code-brackets, why not?

 

Edit: I'm pretty new with this regular expression thing yet..

Link to comment
https://forums.phpfreaks.com/topic/58998-bbcode-code/
Share on other sites

<?php
  function code($string) {
    return preg_replace("/\[bbcode\](.*?)\[\/bbcode\]/", "<code>$1</code>", $string);
  }
  echo code('[bbcode]<span style="color: #ff0000">testing</span>[/bbcode]');
?>

 

Try that. If you end up with HTML being shown and not "testing" in red text then it works.

Link to comment
https://forums.phpfreaks.com/topic/58998-bbcode-code/#findComment-292809
Share on other sites

<?php
  function code($string) {
    return preg_replace("/\[code\](.*?)\[\/code\]/", "<code>$1</code>", $string);
  }
  echo code('[code]<span style="color: #ff0000">testing</span>

');

?>[/code]

 

Try that. If you end up with HTML being shown and not "testing" in red text then it works.

 

It's "Testing" in red.

 

Link to comment
https://forums.phpfreaks.com/topic/58998-bbcode-code/#findComment-292817
Share on other sites

You'll also notice that without using htmlentities() when you echo the converted contents of your string to the browser the code will activate. Instead of using DIV try using <code> and </code> instead.

 

Can't I just do something like:

static function code($string)
{
$code = "/\[code\](.*?)\[\/code\]/is";
$into = "<div id='code'>htmlentities($1)</div>";
$string = preg_replace($code,$into,$string);
return $string;
//return preg_replace("/\[code\](.*?)\[\/code\]/", "<div id='code'>$1</div>", $string);
}

Link to comment
https://forums.phpfreaks.com/topic/58998-bbcode-code/#findComment-292821
Share on other sites

<?php
  function code($string) {
    return preg_replace('/\[bbcode\](.*?)\[\/bbcode\]/is', '<code>$1</code>', $string);
  }
  echo code('[bbcode]<span style="color: #ff0000">testing</span>[/bbcode]');
?>

 

I've changed the bbcode so it doesn't interfere with the forum.

 

Have a go with that and see how it goes.

 

If you can, view the source to the page and post that so we can see exactly what we're getting returned.

Link to comment
https://forums.phpfreaks.com/topic/58998-bbcode-code/#findComment-292835
Share on other sites

And another thing:

static function code($string)
{
$code = "/\[code\](.*?)\[\/code\]/is";
$into = "<div id='code'>htmlspecialchars($1)</div>";
$string = preg_replace($code,$into,$string);
return $string;

//return preg_replace("/\[code\](.*?)\[\/code\]/is", "<div id='code'>". htmlspecialchars("$1") ."</div>", $string);	}

 

How can I get htmlspecialchars to recognize $1 ?

Link to comment
https://forums.phpfreaks.com/topic/58998-bbcode-code/#findComment-292847
Share on other sites

If you want to remove it then you'll need to write a different regex string.

 

What exactly is it you're trying to acieve with your bbcode function?

 

Well, all i want is when i write something like:

<?php

$hello = "hello world";

 

echo $hello;

?>

(Inside code-brackets, I'm excluding them since this forum also uses them)

 

That piece of code should be inside <div id='code'> (or equavilent). And all the code must show on the webpage. I'm not very good at explaining in English, so just ask and I'll try to answer. :)

Link to comment
https://forums.phpfreaks.com/topic/58998-bbcode-code/#findComment-292851
Share on other sites

If yuo try and echo HTML to the browser, the browser will parse it and format it like HTML should be.

 

If you want to display HTML in the browser as plain text then by using <code> you can achieve this without having to convert anything.

 

For example, displaying <b > in a browser will turn bold on but <code> will show <b > as text.

 

(Had to insert a space to HTML bold as the forum turned bold on)

Link to comment
https://forums.phpfreaks.com/topic/58998-bbcode-code/#findComment-292866
Share on other sites

<code>This is a php starter: <?php, and ender: ?>. And this is a line down: <br />. Does it work?</code>

 

That shouldn't give a error, right? Or doesn't it handle php, just html-tags?

I get a php-error (which is quite reasonable), but the br should be deactivated right? I still get a line down when I use br.

Link to comment
https://forums.phpfreaks.com/topic/58998-bbcode-code/#findComment-292877
Share on other sites

Pasting <?php inside is telling the server that you're about to start with some PHP code.

 

To display <?php in a browser you'd need to find some way to add it into the string, maybe like:

echo code('<code>This is a php starter: <'.'?php, and ender: ?'.'>. And this is a line down: <br />. Does it work?</code>');

 

I've split the <?php up so the server doesn't recognise it. I've also had to split the ?> up as well.

Link to comment
https://forums.phpfreaks.com/topic/58998-bbcode-code/#findComment-292882
Share on other sites

Pasting <?php inside is telling the server that you're about to start with some PHP code.

 

To display <?php in a browser you'd need to find some way to add it into the string, maybe like:

echo code('<code>This is a php starter: <'.'?php, and ender: ?'.'>. And this is a line down: <br />. Does it work?</code>');

 

I've split the <?php up so the server doesn't recognise it. I've also had to split the ?> up as well.

 

I think I'll just upload the text to the db with htmlspecialchars. I think that's the best/most easy way to do it in this situation.

Then I don't need to do anything particular clever when I retrieve it from the db, other than putting it inside div's or <code>.

Link to comment
https://forums.phpfreaks.com/topic/58998-bbcode-code/#findComment-292897
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.