John Skoubourdis
John Skoubourdis

Reputation: 3259

How can I highlight many code blocks [code][/code] with PHP?

Hello I have a dynamic text and I want to add code like all the forums like this: [code]My code here...[/code]

Is there an easy way to have a function in PHP to do this stuff?? For example if I write on my text:

Hello world example with PHP [code]echo "hello "world";[/code] and an addition example with PHP [code] echo 5+5; //The result will be 10[/code] etc...

Will apear:

Hello world example with PHP

<?php echo "hello "world"; ?>

and an addition example with PHP

<?php echo 5+5; //The result will be 10 ?>

etc....

Upvotes: 2

Views: 115

Answers (2)

Mob
Mob

Reputation: 11098

In addition, you can use this PHP syntax-highlighting library. Geshi

Upvotes: 2

Matteo Riva
Matteo Riva

Reputation: 25060

Simple case scenario (no nesting, other weird formatting):

$text = preg_replace('#\[code\](.*?)\[\/code\]#s', '<div class="whatever">$1</div>', $text);

Upvotes: 0

Related Questions