clown[NOR] Posted May 3, 2007 Share Posted May 3, 2007 I asked this a while ago, but couldn't get it to work, so I just gave up for a while.. but i'm back what I'm trying to do is to make so that I can enter a php code and it apears like it does here when we use the [ code] [/ code]... I found a code that should do it, but it's not working as I want it to. I've tried modifying the pattern, but since I'm not that good with preg_replace it most of the time results in error messages. the code I'm using is: <?php if (isset($_REQUEST['Submit'])) { $s = $_REQUEST['text']; $s = str_replace("]\n", "]", $s); $match = '#\[code\](.*)\[\/code\]#se'; $replace = "'<div>'.highlight_string(stripslashes('$1'), true).'</div>'"; echo preg_replace($match, $replace, $s); } ?> <form name="code" method="post" action="code.php"> <textarea name="text" cols="100" rows="40"></textarea> <br><input type="submit" name="Submit" value="Submit"> </form> Then I try to use this code in the textarea: <?php if (isset($_REQUEST['Submit'])) { $s = $_REQUEST['text']; $s = str_replace("]\n", "]", $s); $match = '#\[code\](.*)\[\/code\]#se'; $replace = "'<div>'.highlight_string(stripslashes('$1'), true).'</div>'"; echo preg_replace($match, $replace, $s); } ?> But the result ends up like this: <?php if (isset($_REQUEST['Submit'])) { $string = str_replace("]\n\", \"]\", $_REQUEST['text']); $string = str_replace(\"\\\", \"\", $string); $text = preg_replace(\"#\[code\](.*?)\[\/code\]#se\", \"'<div>'.highlight_string(stripslashes('$1'), true).'</div>'\", $string); echo $text; } ?> any ideas on how to solve this one? Thanks In Advance - Clown Quote Link to comment https://forums.phpfreaks.com/topic/49809-help-preg_replace/ Share on other sites More sharing options...
Wildbug Posted May 3, 2007 Share Posted May 3, 2007 I had a slash issue with the /e modifier awhile ago while doing something similar. It tends to add slashes where they're not wanted. Try this: <?php if (isset($_REQUEST['Submit'])) { $_REQUEST['text'] = stripslashes($_POST['text']); $match = array( "/\]\n/", '#\[code\](.*)\[/code\]#se', '/\\\\"/'); $replace = array( ']', '"<div>".highlight_string(\'$1\',true)."</div>"', '"'); echo preg_replace($match, $replace, $_REQUEST['text']); } ?> Quote Link to comment https://forums.phpfreaks.com/topic/49809-help-preg_replace/#findComment-244479 Share on other sites More sharing options...
clown[NOR] Posted May 3, 2007 Author Share Posted May 3, 2007 awsome.. that solved the slash problem, thanks.. but there's one issue left.. how can I stop the code from become red from one point and all the way to the end? like in the last code view in my first post EDIT: i also wonder what the se represents in the pattern Quote Link to comment https://forums.phpfreaks.com/topic/49809-help-preg_replace/#findComment-244535 Share on other sites More sharing options...
Wildbug Posted May 3, 2007 Share Posted May 3, 2007 link=topic=138998.msg589992#msg589992 date=1178214150] EDIT: i also wonder what the se represents in the pattern That was in your original code. They're modifiers. 'e' means treat the replacement like code and use the output of that code as the replacement text. 's' means the dot (.) matches newlines. I'm not sure about the red code, though; it's probably a parsing issue related to quotes. My prefered editor (Crimson Editor) sometimes gets its syntax coloring wrong when it comes to quotes in PHP. Maybe highlight_string() has similar difficulties. Quote Link to comment https://forums.phpfreaks.com/topic/49809-help-preg_replace/#findComment-244556 Share on other sites More sharing options...
clown[NOR] Posted May 3, 2007 Author Share Posted May 3, 2007 oh ok..thanks for the quick answer... do you know anywhere I can read about modifiers? tried lokoing for it on php.net and the manual, but couldn't see anything.. I'm gonna go trough it again... about the red code, thanks for the info... anyone know how to solve the problem? EDIT: Found info about modifiers... Quote Link to comment https://forums.phpfreaks.com/topic/49809-help-preg_replace/#findComment-244563 Share on other sites More sharing options...
MadTechie Posted May 3, 2007 Share Posted May 3, 2007 $match = array( "/\]\n/", '#\[code\](.*)\[/code\]#se', '/\\\\"/'); Matches ] Grabs everything here \\" replaces ] "<div><font color="#0000BB">Grabs everything here</font></div>" of course "Grabs everything here" is formatted with <font color="#0000BB"></font> using the highlight_string can you post some example text.. Quote Link to comment https://forums.phpfreaks.com/topic/49809-help-preg_replace/#findComment-244567 Share on other sites More sharing options...
clown[NOR] Posted May 3, 2007 Author Share Posted May 3, 2007 i'm not sure how that answered my question... can the red code problem have something to do with the modifiers? Quote Link to comment https://forums.phpfreaks.com/topic/49809-help-preg_replace/#findComment-244570 Share on other sites More sharing options...
MadTechie Posted May 3, 2007 Share Posted May 3, 2007 Sorry PC started to reboot so hit post try here $theText = "here is some [ code]sample data[ /code] is this ok";//<--remove spaces $result = preg_replace('~\\[code\\](.*)\\[\\\/code\\]~sim', "highlight_string('$1')", $theText); echo $result; Quote Link to comment https://forums.phpfreaks.com/topic/49809-help-preg_replace/#findComment-244580 Share on other sites More sharing options...
clown[NOR] Posted May 3, 2007 Author Share Posted May 3, 2007 that returned this Warning: preg_replace() [function.preg-replace]: Unknown modifier 'c' in G:\www\test\code.php on line 18 Quote Link to comment https://forums.phpfreaks.com/topic/49809-help-preg_replace/#findComment-244608 Share on other sites More sharing options...
corbin Posted May 3, 2007 Share Posted May 3, 2007 $result = preg_replace('/\[code\](.*)\[\/code\]/')~sim', "highlight_string('$1')", $theText); That should do the trick.... Not sure why MadTechie had all those extra slashes.... Maybe I missed something >.< lol Quote Link to comment https://forums.phpfreaks.com/topic/49809-help-preg_replace/#findComment-244737 Share on other sites More sharing options...
clown[NOR] Posted May 3, 2007 Author Share Posted May 3, 2007 sorry mate.. didn't work... returned the followin: Warning: preg_replace() [function.preg-replace]: Unknown modifier ')' in G:\www\test\code.php on line 16 nevermind: I removed the ) at the end, but the it gave me unknown modifier ~... must be something else Quote Link to comment https://forums.phpfreaks.com/topic/49809-help-preg_replace/#findComment-244758 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.