Jump to content

'Code' tags


HFD

Recommended Posts

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

 

Link to comment
https://forums.phpfreaks.com/topic/137620-code-tags/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.