GuitarGod Posted November 19, 2010 Share Posted November 19, 2010 Hi, I'm trying to create a simple BB code-type function, but I'm having trouble putting a function within preg_replace. Let me show you what I mean: $bb_search = array ( '/\[b\]/', '/\[\/b\]/', '/\[img\=(.*?)\]/', ); $bb_replace = array ( '<b>', '</b>', check_image( '\\1' ), ); return preg_replace( $bb_search, $bb_replace, $text_string ); The first two (b and /b) work fine, even the function is recognised, but using \\1 doesn't put the bbcode value as the function value. I know that probably made no sense - I'm terrible at explaining these things, so sorry. If anyone can understand what I mean, even offer a solution, that would be great! Cheers! Quote Link to comment https://forums.phpfreaks.com/topic/219217-putting-a-function-within-preg_replace/ Share on other sites More sharing options...
simshaun Posted November 19, 2010 Share Posted November 19, 2010 You'd need to use preg_replace_callback. Also, why not use an existing BB code parser? Quote Link to comment https://forums.phpfreaks.com/topic/219217-putting-a-function-within-preg_replace/#findComment-1136789 Share on other sites More sharing options...
ManiacDan Posted November 19, 2010 Share Posted November 19, 2010 Note that there's a comment on the manual page for preg_replace_callback that basically parses bbcode output recursively. -Dan Quote Link to comment https://forums.phpfreaks.com/topic/219217-putting-a-function-within-preg_replace/#findComment-1136793 Share on other sites More sharing options...
AbraCadaver Posted November 19, 2010 Share Posted November 19, 2010 Note that there's a comment on the manual page for preg_replace_callback that basically parses bbcode output recursively. -Dan You probably want to use the e modifier since you only want one pattern to hit the function, not all: $bb_search = array ( '/\[b\]/', '/\[\/b\]/', // add e modifier to have the replacement evaluated as PHP code '/\[img\=(.*?)\]/e', ); $bb_replace = array ( '<b>', '</b>', // quote the function or else it will be called when you define the array not on replacement 'check_image("\\1")', ); Quote Link to comment https://forums.phpfreaks.com/topic/219217-putting-a-function-within-preg_replace/#findComment-1136805 Share on other sites More sharing options...
sasa Posted November 19, 2010 Share Posted November 19, 2010 try <?php $bb_search = array ( '/\[b\]/', '/\[\/b\]/', '/\[img\=(.*?)\]/e', ); $bb_replace = array ( '<b>', '</b>', 'check_image( \\1 )' ); $test = '[img=sasa]'; function check_image($a){ return "<img src='$a'>"; } echo preg_replace($bb_search, $bb_replace, $test); ?> Quote Link to comment https://forums.phpfreaks.com/topic/219217-putting-a-function-within-preg_replace/#findComment-1136824 Share on other sites More sharing options...
GuitarGod Posted November 19, 2010 Author Share Posted November 19, 2010 I used the e modifier and it seems to work. Thanks a lot! Also, why not use an existing BB code parser? I'm creating some basic software with a friend and we like to keep all code original. Quote Link to comment https://forums.phpfreaks.com/topic/219217-putting-a-function-within-preg_replace/#findComment-1136845 Share on other sites More sharing options...
simshaun Posted November 19, 2010 Share Posted November 19, 2010 And does your original code take into account somebody leaving tags unclosed? Sometimes its not best to re-invent the wheel. Quote Link to comment https://forums.phpfreaks.com/topic/219217-putting-a-function-within-preg_replace/#findComment-1136848 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.