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
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
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
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
Share on other sites

That's your choice kilma. Your code, our opinions, you decide.

 

Hehe, well, that's good :)

 

But I really don't see the downside with my way. But I get the feel that you guys have been in the game longer than me ;) So all opinions noted. :)

Link to comment
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
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
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
Share on other sites

Why don't you use <code> instead of <div>? That way you won't have to worry about htmlspecialchars()

 

Maybe it's something I've overlooked, but how would that help?

 

Does <code> do anything extra than just being a html style tag?

Link to comment
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
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
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
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
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.