Northern Flame Posted February 24, 2008 Share Posted February 24, 2008 I want to create something like what this website has and am curious as to what would be the best way to do this. what i want to create is a way for users to type in something like [ code ] [ /code ] and in between those brackets type in php code and have it output like this: <?php $hello = "Hello World!"; echo $hello; ?> what would be the best way to do this? would i just create a function like this: <?php function makePHPcode($input){ $input = str_replace('if', '<font color="blue">IF</font>', $input); // And repeat this for all the other possible php commands $div_beg = '<div style="background:#888888;">'; $div_end = '</div>'; return $div_beg . $input . $div_end; } $input = $_POST['input']; makePHPcode($input); ?> can someone let me know if theres a faster way of doing this? Link to comment https://forums.phpfreaks.com/topic/92697-php-user-input-add-color/ Share on other sites More sharing options...
roopurt18 Posted February 24, 2008 Share Posted February 24, 2008 If you don't know anything about parsers, grammars, tokenizers, lexical analyzers, FSMs, etc. then yes, using some combination of str_replace and / or preg_replace will probably be the easiest solution. Otherwise, what you're wanting to do is very similar to the first couple steps of writing a compiler. Link to comment https://forums.phpfreaks.com/topic/92697-php-user-input-add-color/#findComment-474977 Share on other sites More sharing options...
Northern Flame Posted February 24, 2008 Author Share Posted February 24, 2008 ill look that up, thanks for the reply Link to comment https://forums.phpfreaks.com/topic/92697-php-user-input-add-color/#findComment-474979 Share on other sites More sharing options...
Barand Posted February 24, 2008 Share Posted February 24, 2008 or www.php.net/highlight_string $str = '<?php $x = "Hello World"; echo $str . "<br/>"; ?>'; highlight_string($str); Link to comment https://forums.phpfreaks.com/topic/92697-php-user-input-add-color/#findComment-475016 Share on other sites More sharing options...
roopurt18 Posted February 24, 2008 Share Posted February 24, 2008 Heh, totally wasn't aware of that function. I read some of the comments on that function's documentation page and plenty of other users have written there own functions. I checked out a function on some guy's blog and also stumbled across the GeSHI (Generic Syntax Highlighter) project. Basically it doesn't look like you have to write this yourself. If you want to see how good a syntax highlighter is, give it something tricky: <p>This is HTML! It has reserved words: if, do, while, etc. It also has $variables!</p> <?php echo "<p> This is a tricky PHP string!. It contains reserved words: if, do, while, end, break, foreach, etc. It also contains tricky characters: \", \\, \n. It also contains {$Clean['variables']} and $foobar! </p>"; ?> <p>We're back in HTML!</p> (And just to see how the SMF forums handle it) <p>This is HTML! It has reserved words: if, do, while, etc. It also has $variables!</p> <?php echo "<p> This is a tricky PHP string!. It contains reserved words: if, do, while, end, break, foreach, etc. It also contains tricky characters: \", \\, \n. It also contains {$Clean['variables']} and $foobar! </p>"; ?> <p>We're back in HTML!</p> Link to comment https://forums.phpfreaks.com/topic/92697-php-user-input-add-color/#findComment-475227 Share on other sites More sharing options...
Northern Flame Posted February 24, 2008 Author Share Posted February 24, 2008 lol thanks for all the help! Link to comment https://forums.phpfreaks.com/topic/92697-php-user-input-add-color/#findComment-475358 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.