Yo can do that by simple regular replace:
function code($match) {
$geshi = new GeSHi($match[2], $match[1]);
return $geshi->parse_code();
}
$html = preg_replace_callback('~\[code=(.+?)\](.+?)\[\/code\]~is', 'code', $text);
Question
I'm making a blog from scratch in PHP + MySQL, where I will sometimes post lines of PHP codes for tutorials and so on... So, I installed the GeSHi class in my project. But since I will write my blogs/articles in normal text format, I will write up some tutorials like:
Here is some PHP code: [code=php] <?php echo 'Hello World!'; ?> [/code]
And this will be stored on my MySQL DB in TEXT format, so how can I set PHP to recognize code's and use the GeSHi class to parse it?
Solution
Yo can do that by simple regular replace:
function code($match) {
$geshi = new GeSHi($match[2], $match[1]);
return $geshi->parse_code();
}
$html = preg_replace_callback('~\[code=(.+?)\](.+?)\[\/code\]~is', 'code', $text);
OTHER TIPS
There are two ways :
preg_replace_callback('#\[code=([a-zA-Z]+)](.+)\[/code]#sU',function($matches){
$geshi = new GeShi($matches[1]);
return $geshi->parse($matches[2]);
},
$text );
http://php.net/manual/fr/book.bbcode.php
$bbcodeContainer = bbcode_create(array(
''=> array('type'=>BBCODE_TYPE_ROOT, 'childs'=>'!i'),
'i'=> array('type'=>BBCODE_TYPE_NOARG, 'open_tag'=>'<i>',
'close_tag'=>'</i>', 'childs'=>'b'),
'url'=> array('type'=>BBCODE_TYPE_OPTARG,
'open_tag'=>'<a href="{PARAM}">', 'close_tag'=>'</a>',
'default_arg'=>'{CONTENT}',
'childs'=>'b,i'),
'img'=> array('type'=>BBCODE_TYPE_NOARG,
'open_tag'=>'<img src="', 'close_tag'=>'" />',
'childs'=>''),
'b'=> array('type'=>BBCODE_TYPE_NOARG, 'open_tag'=>'<b>',
'close_tag'=>'</b>'),
));
bbcode_add_element($bbcodeContainer,'code',array('type'=>BBCODE_TYPE_OPTARG,
'open_tag'=>'<div class="code">','close_tag'=>'</div>',
'content_handling'=>function($content,$params){
$geshi = new GeShi($params[0]);
return $geshi->parse($content);
}));