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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.