kimla Posted July 9, 2007 Share Posted July 9, 2007 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.. Quote Link to comment Share on other sites More sharing options...
Yesideez Posted July 9, 2007 Share Posted July 9, 2007 You need to pass the string containing the bbcode in $string in your preg_replace() Quote Link to comment Share on other sites More sharing options...
marcus Posted July 9, 2007 Share Posted July 9, 2007 $code = "'/\[code\](.*?)\[\/code\]/is"; $into = "<div id='code'>$1</div>"; $string = preg_replace($code,$into,$string); Quote Link to comment Share on other sites More sharing options...
Yesideez Posted July 9, 2007 Share Posted July 9, 2007 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. Quote Link to comment Share on other sites More sharing options...
kimla Posted July 9, 2007 Author Share Posted July 9, 2007 I modified it to: $code = "/\[code\](.*?)\[\/code\]/is"; It gave an error when you had the ' in the beginning there. But it seems to be working now, what is the /is at the end? Quote Link to comment Share on other sites More sharing options...
Yesideez Posted July 9, 2007 Share Posted July 9, 2007 <?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. Quote Link to comment Share on other sites More sharing options...
marcus Posted July 9, 2007 Share Posted July 9, 2007 It means it's case-INsensitive. So they could type it like: [ C o D E ] and it will still turn into <div id='code'> Quote Link to comment Share on other sites More sharing options...
kimla Posted July 9, 2007 Author Share Posted July 9, 2007 <?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. Quote Link to comment Share on other sites More sharing options...
marcus Posted July 9, 2007 Share Posted July 9, 2007 Might I add, if you're showing code I would use htmlspecialchars so <'s would be < >'s would be >, etc...... Quote Link to comment Share on other sites More sharing options...
kimla Posted July 9, 2007 Author Share Posted July 9, 2007 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); } Quote Link to comment Share on other sites More sharing options...
kimla Posted July 9, 2007 Author Share Posted July 9, 2007 Might I add, if you're showing code I would use htmlspecialchars so <'s would be < >'s would be >, etc...... Or replace htmlentities with htmlspecialchars.. Quote Link to comment Share on other sites More sharing options...
marcus Posted July 9, 2007 Share Posted July 9, 2007 That's your choice kilma. Your code, our opinions, you decide. Quote Link to comment Share on other sites More sharing options...
kimla Posted July 9, 2007 Author Share Posted July 9, 2007 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. Quote Link to comment Share on other sites More sharing options...
Yesideez Posted July 9, 2007 Share Posted July 9, 2007 <?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. Quote Link to comment Share on other sites More sharing options...
kimla Posted July 9, 2007 Author Share Posted July 9, 2007 <code><span style="color: #ff0000">testing</span></code> So it is working, but should the <span> be removed by the <code> segment? I don't think I understand this completely.. ??? Quote Link to comment Share on other sites More sharing options...
Yesideez Posted July 9, 2007 Share Posted July 9, 2007 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? Quote Link to comment Share on other sites More sharing options...
kimla Posted July 9, 2007 Author Share Posted July 9, 2007 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 ? Quote Link to comment Share on other sites More sharing options...
kimla Posted July 9, 2007 Author Share Posted July 9, 2007 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. Quote Link to comment Share on other sites More sharing options...
Yesideez Posted July 9, 2007 Share Posted July 9, 2007 Why don't you use <code> instead of <div>? That way you won't have to worry about htmlspecialchars() Quote Link to comment Share on other sites More sharing options...
kimla Posted July 9, 2007 Author Share Posted July 9, 2007 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? Quote Link to comment Share on other sites More sharing options...
Yesideez Posted July 9, 2007 Share Posted July 9, 2007 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) Quote Link to comment Share on other sites More sharing options...
Yesideez Posted July 9, 2007 Share Posted July 9, 2007 <?php echo '<b>'; //Sends <b> to the browser but turns bold on echo '<code><b></code>'; //shows <b> in the browser as text ?> Quote Link to comment Share on other sites More sharing options...
kimla Posted July 9, 2007 Author Share Posted July 9, 2007 <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. Quote Link to comment Share on other sites More sharing options...
Yesideez Posted July 9, 2007 Share Posted July 9, 2007 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. Quote Link to comment Share on other sites More sharing options...
kimla Posted July 9, 2007 Author Share Posted July 9, 2007 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>. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.