4

I am wondering is it possible to print php syntax on html page.For example below is the php syntax i want to echo on html page via php.

Please <?php echo $random[array_rand($random)]; ?> the [url=http://feeds.feedburner.com]hello[/url].<br>
Also [url=http://feeds.feedburner.com]<?php echo $random[array_rand($random)]; ?>[/url].
<br> 

5 Answers 5

13

Try this:

<?php

$str='Please <?php echo $random[array_rand($random)]; ?> the [url=http://feeds.feedburner.com]hello[/url].<br>
Also [url=http://feeds.feedburner.com]<?php echo $random[array_rand($random)]; ?>[/url].
<br>';

echo highlight_string($str);

?>

Output:

highlights

Sign up to request clarification or add additional context in comments.

3 Comments

I'd been using exec( "php -s ..." ) all this time :S
Interesting function... Is it possible to replace style="..." with class="..." and manually add styles for classes?
Check out this. I haven't seen anything that can change style directly, and I wouldn't want to parse html either.
1

use htmlspecialchars for print Php syntax in html

echo htmlspecialchars('<?php echo "Hello, World!"; ?>');

Output

<?php echo "Hello, World!"; ?>

Demo

1 Comment

can i get online generator for this ?
1

Use highlight_string to highlight syntax

Comments

0

Use character references for characters with special meaning.

&lt;php echo etc etc

Comments

0

If you want to print it from a file, use

highligh_code($filename);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.