Prismatic Posted March 27, 2006 Share Posted March 27, 2006 Ok, I have a problem :( on my forums I have code blocks. These blocks accept all code and show the proper indents and whatnot. My whole code to do the matching and replacing is:[code]if (strstr($text, "[bbcode]")){ $str = str_replace("]\n", "]", $text); //Pattern for Preg_Replace $match = array('@\[bbcode\](.*?)\[\/bbcode\]@esi'); //Pattern for Preg_Match_All $match_String = '@\[bbcode\](.*?)\[\/bbcode\]@esi'; //preg_match($match_String, $text, $matches, PREG_OFFSET_CAPTURE); preg_match_all($match_String, $str, $matches, PREG_SET_ORDER); //Iterate through the array $matches foreach($matches as $key){ $Value = $key[1]; //So code indenting works. $Value = str_replace(" ", " ", $Value); $Value = str_replace(" ", " ", $Value); //The character ")" was causing an issue. $Value = str_replace(")", ")", $Value); //Replace array. $replace = array("'<table width=\"95%\" align=\"center\" border=\"0\"><tr><td class=\"BoardColumn\"><a class=\"SmallText_Code_Block\"><b>Code Block</b></a></td></tr><tr><td class=\"MainMenuRow\"><a class=\"SmallText\">$Value</a></td></tr></table>'"); //Show the block in all it's glory $text= preg_replace($match, $replace, $text, 1); } } return $text;[/code]Thing is, to make preg_replace work I need to first change ALL html chars to the resulting html codes. I do that when the post is made with:[code]/* Clean up the post to make it good for storing in the database */ $body = htmlentities($_POST[message_body]); $body = str_replace("(", "(", $body); $body = str_replace(")", ")", $body); $body = str_replace("'", "'", $body); $body = str_replace("$", "$", $body); $body = str_replace("_", "_", $body); $body = str_replace("Y", "Υ", $body);[/code]All those str_replaces were causing the preg_replace to fail, dont know why, they just were. Posting code works fine as it is, it gives no errors or anything, except now I want to highlight the code. This is causing a problem since the origonal code has been bastardized to get it to pass through the preg_replace without causing an error. IE:this posted[code]A 'quote' is <b>bold</b>[/code]turns into this:[code]A 'quote' is <b>bold</b>[/code]It displays fine, but code highlighting is killed in the process.Is there any way to make preg_replace accept those html special chars without giving an error? This is the kind of error returned if I try to have it process code normally:[code]Parse error: parse error, unexpected '<' in functions.php(486) : regexp code on line 3Fatal error: preg_replace() [function.preg-replace]: Failed evaluating code: '<table width="95%" align="center" border="0"><tr><td class="BoardColumn"><a class="SmallText_Code_Block"><b>Code Block</b></a></td></tr><tr><td class="MainMenuRow"><a class="SmallText"> echo "test!'; </a></td></tr></table>' in functions.php on line 486[/code] Quote Link to comment https://forums.phpfreaks.com/topic/5891-preg_replace-does-not-accept-html-chars/ Share on other sites More sharing options...
shoz Posted March 27, 2006 Share Posted March 27, 2006 [a href=\"http://www.php.net/preg_quote\" target=\"_blank\"]preg_quote[/a] may be what you're looking for. Quote Link to comment https://forums.phpfreaks.com/topic/5891-preg_replace-does-not-accept-html-chars/#findComment-21030 Share on other sites More sharing options...
Prismatic Posted March 27, 2006 Author Share Posted March 27, 2006 Would that prevent error's when preg_replace tries to process strings that contain characters such as < > / ( ) ; and stuff like that? Quote Link to comment https://forums.phpfreaks.com/topic/5891-preg_replace-does-not-accept-html-chars/#findComment-21031 Share on other sites More sharing options...
shoz Posted March 27, 2006 Share Posted March 27, 2006 [quote]Would that prevent error's when preg_replace tries to process strings that contain characters such as < > / ( ) ; and stuff like that?[/quote]It should.Post the changes you've made, when you're finished. Quote Link to comment https://forums.phpfreaks.com/topic/5891-preg_replace-does-not-accept-html-chars/#findComment-21041 Share on other sites More sharing options...
Prismatic Posted March 27, 2006 Author Share Posted March 27, 2006 [!--quoteo(post=358733:date=Mar 26 2006, 08:53 PM:name=shoz)--][div class=\'quotetop\']QUOTE(shoz @ Mar 26 2006, 08:53 PM) [snapback]358733[/snapback][/div][div class=\'quotemain\'][!--quotec--]It should.Post the changes you've made, when you're finished.[/quote]Well, preg_quote gives me "\Test\<\/b\>" when I insert "<b>Test</b>" into the database..WITHOUT preg_quote the word Test shows up bold (NOT what I want, I do NOT want the code to be executed, huge security risk)Ive come to the conslusion it's impossible to get the best of both worlds. I can either get code blocks with no highlighting or not. Since the only way I can process code through preg_replace is by running all this on it before I send it off to the database:[code] $body = htmlentities($_POST[message_body]); $body = str_replace("(", "(", $body); $body = str_replace(")", ")", $body); $body = str_replace("'", "'", $body); $body = str_replace("$", "$", $body); $body = str_replace("_", "_", $body); $body = str_replace("Y", "Υ", $body); $body = str_replace(")", ")", $body);[/code]:( All those characters you see above were causing preg_replace to crash when it tried processing. Dont ask me, it just sucks I guess :|Edit - nice, cant even post my real code :| all the replacment values for thsoe str_replaces are their respective html codes like &.#933; for Y (Without the period). Quote Link to comment https://forums.phpfreaks.com/topic/5891-preg_replace-does-not-accept-html-chars/#findComment-21057 Share on other sites More sharing options...
shoz Posted March 27, 2006 Share Posted March 27, 2006 When looking through your question the first time I skimmed over it. Try removing the "e" from @esi in [code] //Pattern for Preg_Replace $match = array('@\[bbcode\](.*?)\[\/bbcode\]@esi'); //Pattern for Preg_Match_All $match_String = '@\[bbcode\](.*?)\[\/bbcode\]@esi';[/code]Remember to remove/comment the code you've been using to change <> etc into &#etc Quote Link to comment https://forums.phpfreaks.com/topic/5891-preg_replace-does-not-accept-html-chars/#findComment-21069 Share on other sites More sharing options...
Prismatic Posted March 27, 2006 Author Share Posted March 27, 2006 [!--quoteo(post=358761:date=Mar 26 2006, 10:53 PM:name=shoz)--][div class=\'quotetop\']QUOTE(shoz @ Mar 26 2006, 10:53 PM) [snapback]358761[/snapback][/div][div class=\'quotemain\'][!--quotec--]When looking through your question the first time I skimmed over it. Try removing the "e" from @esi in [code] //Pattern for Preg_Replace $match = array('@\[bbcode\](.*?)\[\/bbcode\]@esi'); //Pattern for Preg_Match_All $match_String = '@\[bbcode\](.*?)\[\/bbcode\]@esi';[/code]Remember to remove/comment the code you've been using to change <> etc into &#etc[/quote]Removed the E and the only thing that changes is I am now getting a ' above and below my code block. Text is still showing up bold (IE - It's running - big nono)[code] if (strstr($text, "[bbcode]")){ $str = str_replace("]\n", "]", $text); //Pattern for Preg_Replace $match = array('@\[bbcode\](.*?)\[\/bbcode\]@si'); //Pattern for Preg_Match_All $match_String = '@\[bbcode\](.*?)\[\/bbcode\]@si'; //preg_match($match_String, $text, $matches, PREG_OFFSET_CAPTURE); preg_match_all($match_String, $str, $matches, PREG_SET_ORDER); //Iterate through the array $matches foreach($matches as $key){ $Value = $key[1]; //So code indenting works. $Value = str_replace(" ", " ", $Value); $Value = str_replace(" ", " ", $Value); $Value = preg_quote($Value, '/'); //Replace array. $replace = array("'<table width=\"95%\" align=\"center\" border=\"0\"><tr><td class=\"BoardColumn\"><a class=\"SmallText_Code_Block\"><b>Code Block</b></a></td></tr><tr><td class=\"MainMenuRow\"><a class=\"SmallText\">$Value</a></td></tr></table>'"); //Show the block in all it's glory $text = preg_replace($match, $replace, $text, 1); } } return $text;[/code][img src=\"http://www.warrency.com/codeblock.JPG\" border=\"0\" alt=\"IPB Image\" /]^^ What it looks like with preg_quote on.. Quote Link to comment https://forums.phpfreaks.com/topic/5891-preg_replace-does-not-accept-html-chars/#findComment-21077 Share on other sites More sharing options...
shoz Posted March 27, 2006 Share Posted March 27, 2006 Remove the preg_quote line. I offered it as a solution originally when I assumed to some degree what the error was because of how similar the problem is to one that preg_quote would solve.Also change[code]class=\"SmallText\">$Value</a></td></tr></table>'"//toclass=\"SmallText\">".htmlentities($Value)."</a></td></tr></table>'"[/code] Quote Link to comment https://forums.phpfreaks.com/topic/5891-preg_replace-does-not-accept-html-chars/#findComment-21080 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.