Jump to content

PHP - User Input - Add Color....


Northern Flame

Recommended Posts

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

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

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