HFD Posted December 19, 2008 Share Posted December 19, 2008 Hi, my site is a resource for web designers featuring articles/tutorials and such. I've made a basic administration system where an article writer can write an article. On occasions the writer may need to write 'code' out, so I made a basic 'code' class, and used a preg_replace (which people on this forum helped me with) to change [ code] into <div class="code">..etc. Here is the code for this: $content = preg_replace('#\[code\](.*?)\[/code\]#ies', "'<div class=\"code\">'.htmlspecialchars('$1').'</div>'", $content); Only thing is I have two problems. As I'm using htmlspecialchars it doesn't pick up on REAL line breaks when an article writer wants to say, create a new line in the code DIV. One way of getting around this was using white-space: pre - however that caused more trouble, as text wouldn't wrap so long code would trail off the page to the right. I've been considering perhaps using the 'nl2br' function to convert line breaks to BR's, however how would I do this ONLY for text between the code tags? The $content variable contains all the articles content, so I assume I'll need to search that. Can anyone help? Thanks Quote Link to comment https://forums.phpfreaks.com/topic/137620-code-tags/ Share on other sites More sharing options...
.josh Posted December 19, 2008 Share Posted December 19, 2008 You could preg_match_all using the same pattern as the preg_replace, do what you need to do on the matches (like nl2br) and then use the results of that as the replacement in your preg_replace. . Quote Link to comment https://forums.phpfreaks.com/topic/137620-code-tags/#findComment-719342 Share on other sites More sharing options...
phpian Posted December 19, 2008 Share Posted December 19, 2008 have you tried.... $content = preg_replace('#\[code\](.*?)\[/code\]#ies', "'<div class=\"code\">'.nl2br(htmlspecialchars('$1')).'</div>'", $content); Quote Link to comment https://forums.phpfreaks.com/topic/137620-code-tags/#findComment-719343 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.