tiki Posted June 2, 2006 Share Posted June 2, 2006 Ok my function is supposed to select all the text inbetween <pre> and </pre> and then apply htmlspecialchars to it. Its used for a tutorial system, but it is not working.Here is what I have so far, the before and after arent used at the moment.[code]function tutorialIt($data) {// Changes the <>"" to the html ones $before = array('<','>','"'); $after = array('<','>','"'); $data = preg_replace("/<pre>(.*?)<\/pre>/", "stripslashes(htmlspecialchars($1))", $data); echo $data;}[/code]Help? Quote Link to comment https://forums.phpfreaks.com/topic/10995-regex-and-preg_replace-help/ Share on other sites More sharing options...
poirot Posted June 2, 2006 Share Posted June 2, 2006 You don't need regular expressions to do that. Something like this:[code]function tutorialIt($data) { $content = htmlspecialchars($data, ENT_NOQUOTES); echo '<pre>' . $content . '</pre>';}[/code]Here is what htmlspecialchars() does:[!--quoteo--][div class=\'quotetop\']QUOTE[/div][div class=\'quotemain\'][!--quotec--]'&' (ampersand) becomes '&' '"' (double quote) becomes '"' when ENT_NOQUOTES is not set. ''' (single quote) becomes ''' only when ENT_QUOTES is set. '<' (less than) becomes '<' '>' (greater than) becomes '>' [/quote][a href=\"http://www.php.net/htmlspecialchars\" target=\"_blank\"]http://www.php.net/htmlspecialchars[/a] Quote Link to comment https://forums.phpfreaks.com/topic/10995-regex-and-preg_replace-help/#findComment-41068 Share on other sites More sharing options...
tiki Posted June 2, 2006 Author Share Posted June 2, 2006 But its for this, [a href=\"http://www.mileswjohnson.com/tutorials/1/the-basics-of-xhtml/\" target=\"_blank\"]http://www.mileswjohnson.com/tutorials/1/t...asics-of-xhtml/[/a]The whole tutorial is text, yet I want only the text inbetween the pre tags to be formatted.View the source and you will see. Quote Link to comment https://forums.phpfreaks.com/topic/10995-regex-and-preg_replace-help/#findComment-41076 Share on other sites More sharing options...
poirot Posted June 2, 2006 Share Posted June 2, 2006 Oh, sure:[code]<?php$data = '<html><title>Test</title><span style="color: blue">TutoriaL</span><br /><br />Writing bold text:<pre><b>This is a test code</b></pre>Simple Javascript alert:<pre><script type="text/javascript"> alert("Hello There!");</script></pre>';function convertHTML($matches){ $before = array('<','>','"'); $after = array('<','>','"'); return '<pre>' . str_replace($before, $after, $matches[1]) . '</pre>';}function tutorialIt($data) { $data = preg_replace_callback("/<pre>(.*?)<\/pre>/s", 'convertHTML', $data); echo $data;}tutorialIt($data);?>[/code]Which outputs something like this (note that the forum may screw it):[code]<html><title>Test</title><span style="color: blue">TutoriaL</span><br /><br />Writing bold text:<pre><b>This is a test code</b></pre>Simple Javascript alert:<pre><script type="text/javascript"> alert("Hello There!");</script></pre>[/code] Quote Link to comment https://forums.phpfreaks.com/topic/10995-regex-and-preg_replace-help/#findComment-41218 Share on other sites More sharing options...
tiki Posted June 3, 2006 Author Share Posted June 3, 2006 Thank you very much, it worked! <3 Quote Link to comment https://forums.phpfreaks.com/topic/10995-regex-and-preg_replace-help/#findComment-41360 Share on other sites More sharing options...
poirot Posted June 3, 2006 Share Posted June 3, 2006 Hmmm... It's the second time I see this...What does "<3" means? Is it s smilie? Quote Link to comment https://forums.phpfreaks.com/topic/10995-regex-and-preg_replace-help/#findComment-41376 Share on other sites More sharing options...
tiki Posted June 3, 2006 Author Share Posted June 3, 2006 Its a heart, so I basically love you. Lol Quote Link to comment https://forums.phpfreaks.com/topic/10995-regex-and-preg_replace-help/#findComment-41398 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.