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? 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] 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. 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] 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 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? 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 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
Archived
This topic is now archived and is closed to further replies.