简体   繁体   中英

PHP: htmlentities/strip_tags

I've been re-writing my website lately and added a Syntax highlighter so that I can post code snippets. Before, all I did was htmlentities() the string so that it would be safe and not break anything, but now that I have to use a <pre> to highlight code, htmlentites() effectively removes the syntax highlighting from the page. I've been trying to come up with a function that will just perform an htmlentites() on anything between two tags ( <entitiesparse> </entitiesparse> ) but nothing seems to work. Does anyone know of a function that I can either:

a) Set it to htmlentities() everything but specific tags (like strip_tags() )

OR

b) Only htmlentities() things in certain tags (As mentioned above)

You only need to apply htmlentities() to the raw content. So you can apply htmlentities() to the raw content (the article text) and then invoke a function to add syntax highlighting after that. So long as you check that your syntax highlighting code cannot introduce unexpected nasties, you don't need to call htmlentities() again.

And if you're saying that you use the a element to highlight code, I strongly suggest you use the code element instead, which is designed to provide markup for lines or blocks of programming code. The a element should only be used as an anchor for a hyperlink.

For instance, you could use

<code class="highlighted-code">/* line of code here /*</code>

Then you could use a cascading style sheet to provide background colour for any element of type code with class equal to "highlighted-code", for instance:

code.highlighted-code {background-color: yellow}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM