Gingechilla Posted February 25, 2011 Share Posted February 25, 2011 My code for replacing user input is as follows: function Replace_BB($text) { $bb = array( '@\[u\](.*?)\[\/u\]@is', '@\[i\](.*?)\[\/i\]@is', '@\[b\](.*?)\[\/b\]@is', '@\[img\](.*?)\[/img\]@is', '@\[url\](.*?)\[/url\]@is', '@\[url=http://(.*?)\](.*?)\[/url\]@is' ); $html = array( '<u>$1</u>', '<em>$1</em>', '<strong>$1</strong>', '<img src="$1" />', '<a href="$1">$1</a>', '<a href="$1">$2</a>' ); return preg_replace($bb, $html, $text); } print_r (Replace_BB($_POST['data'])); How can I use an if statement to decide how to put in my replacement. So for example: $html = array( 'IF XXXXXX THEN DO THIS>>>>> <u>$1</u> ELSE DO THIS >>>> <u>$1 Error</u>', Link to comment https://forums.phpfreaks.com/topic/228850-if-statements-inside-array-replacement/ Share on other sites More sharing options...
DavidAM Posted February 25, 2011 Share Posted February 25, 2011 Have a look at the "e" modifier for replacement strings. function Replace_BB($text) { $bb = array( '@\[u\](.*?)\[\/u\]@ise', // ADDED THE e MODIFIER TO THIS ONE '@\[i\](.*?)\[\/i\]@is', '@\[b\](.*?)\[\/b\]@is', '@\[img\](.*?)\[/img\]@is', '@\[url\](.*?)\[/url\]@is', '@\[url=http://(.*?)\](.*?)\[/url\]@is' ); $html = array( '"checkU(\'$1\')"', // CHANGED THE REPLACEMENT WITH A FUNCTION CALL '<em>$1</em>', '<strong>$1</strong>', '<img src="$1" />', '<a href="$1">$1</a>', '<a href="$1">$2</a>' ); return preg_replace($bb, $html, $text); } /* ADDED Callback function for preg_replace() */ function checkU($text) { if ($text == 'something') return '<u>' . $text . ' Error</u>'; else return '<u>' . $text . '</u>'; } print_r (Replace_BB($_POST['data'])); Link to comment https://forums.phpfreaks.com/topic/228850-if-statements-inside-array-replacement/#findComment-1179780 Share on other sites More sharing options...
Gingechilla Posted February 25, 2011 Author Share Posted February 25, 2011 Thank you very much for your reply however I don't think it has worked properly. HTML Input: [u]Hello[/u] HTML Output: checkU('Hello') Link to comment https://forums.phpfreaks.com/topic/228850-if-statements-inside-array-replacement/#findComment-1179786 Share on other sites More sharing options...
Gingechilla Posted February 25, 2011 Author Share Posted February 25, 2011 Oh I've got it! I just had to remove the " " either side of checkU in the replacement code. Thank you so much for your help! :-) Link to comment https://forums.phpfreaks.com/topic/228850-if-statements-inside-array-replacement/#findComment-1179790 Share on other sites More sharing options...
DavidAM Posted February 26, 2011 Share Posted February 26, 2011 Oops, sorry about that, you're right the double quotes inside the replacement string did NOT belong there. Glad you figured it out. Link to comment https://forums.phpfreaks.com/topic/228850-if-statements-inside-array-replacement/#findComment-1179804 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.