将代码输出为php中的文本 [英] Output code as text in php

查看:24
本文介绍了将代码输出为php中的文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

基本上,我想要一些类似于 mybb 在 php 中所做的事情,他们将在 [code][/code] 标签之间输出代码作为突出显示的文本.显然我需要一些正则表达式,但是将所有 php 代码输出为纯文本的关键是什么,尤其是在存储在 db 中之后.

不仅仅是 php 代码,而是所有的纯文本代码

解决方案

IMO,最简单的方法是:

1) 将文本读入字符串

2) 或者,运行 htmlspecialchars()

3a) echo() 字符串

3b) 一定要在字符串前后添加

标签

Basically, I want to to something similar as to what mybb does in php, where they will output code between [code][/code] tags as highlighted text. Obviously I will need some regular expressions, but what's the key to outputting all php code as plain text, especially after stored in a db.

Edit: not just php code but all code as plain text

解决方案

IMO, the easiest way would be to:

1) Read your text into a string

2) Optionally, run htmlspecialchars()

3a) echo() the string

3b) be sure to add <pre> and </pre> tags before and after the string

这篇关于将代码输出为php中的文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆