jackpf Posted February 16, 2009 Share Posted February 16, 2009 Hi all, first post. Basically, my site has bbcode style regular expressions. However, I was wondering if there's a way to call a function on an extracted regular expression? For example, <?php function replacewithfunction($str) { $exist = '/\[code lang\=(.*?)\](.*?)\[\/code\]/is'; $replace = geshi_code($1, $2); $return = preg_replace($exist, $replace, $str); return $return; } ?> Where geshi_code() is the function I wish to call. Obviously this returns a syntax error, but this is basically what I want do. Any help would be much appreciated. Thanks, Jack. Quote Link to comment https://forums.phpfreaks.com/topic/145358-calling-a-function-on-regular-expressions/ Share on other sites More sharing options...
gevans Posted February 16, 2009 Share Posted February 16, 2009 Take a look at this page on the php.net site STICKY You should be able to use that with a bit more work to achieve what you need Quote Link to comment https://forums.phpfreaks.com/topic/145358-calling-a-function-on-regular-expressions/#findComment-763072 Share on other sites More sharing options...
jackpf Posted February 16, 2009 Author Share Posted February 16, 2009 Hmm...had a look. I've now got this- <?php function geshi_code($lang, $source) { include('geshi/geshi.php'); $path = 'geshi/geshi/'; $geshi = new GeSHi($source, $lang, $path); return $geshi->parse_code(); } function str_code($str) { $str = htmlspecialchars($str); $exist = '/\[somecode lang\=(.*?)\](.*?)\[\/somecode\]/is'; $replace = geshi_code("$1", "$2"); return preg_replace($exist, $replace, $str); } echo str_code("This is some code.[somecode lang=php]<?php $var = 'hello'; ?>[/somecode] end of code."); ?> But it returns this- This is some code. <?php $var = 'hello'; ?> end of code. Unhighlighted. In case you didn't know, geshi is a syntax highlighter. I don't understand why it's not working... Thanks anyway. Quote Link to comment https://forums.phpfreaks.com/topic/145358-calling-a-function-on-regular-expressions/#findComment-763078 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.