【发布时间】:2014-04-06 11:12:30
【问题描述】:
我已经为此苦苦挣扎了好几个小时。 已尝试在线搜索解决方案,但我发现的一切都不是我想要的。迫切希望得到答案。
我有一个包含文本的长字符串(mySQL 文本字段)。 虚拟文本示例:
There are many possible ways to minimize chances of cancer.
One such which has been proven beneficial is the use of anti oxidants
and various supplements.
[ARTICLE]
[TITLE]Green tea shows strong anti oxidant effects[/TITLE]
[DATE]Article published on May 2005[/DATE]
[BY]Department of Oncology research, University Hospital Denmark[/BY]
[TEXT]We test 54 subjects and given several vitmins,
other group received placebo. [[MARK]]We concluded that green
tea is an effective anti oxidant[[/MARK]]. We found Vitamin C to be
less effective.[/TEXT]
[/ARTICLE]
We also tested other supplements and also found interesting properties.
[ARTICLE]
[TITLE]Carrots ineffective for testicular cancer[/TITLE]
[DATE]Article published on October 2012[/DATE]
[BY]Oncology Journal, issue: 54[/BY]
[TEXT]Many people carrots are effective for several types of cancer.
In the research we did [[MARK]]we found that Carrots did lower
the cancer markers in test subjects[[/MARK]]. We cannot recommend
it as anti cancer.[/TEXT]
[/ARTICLE]
We will publish more research later on."
我需要做的是解析该字符串并输出它。 请注意,字符串有两个部分(每个部分都以 [ARTICLE] 开头并以 [/ARTICLE] 结尾,并且每个部分都有内部自定义标签。 对于每个以 [ARTICLE] 开头并以 [/ARTICLE] 结尾的地方,我不会按原样输出,而是要调用我的自定义函数以不同的格式。
例如:
function format_text_with_articles (ArtTitle, ArtDate, ArtBy, ArtText){
// This is just a simple function I already have that gets
// the params and formats a table with special formatting inside that makes
// article extracts look nice.
}
所以只需将所有文本输出到浏览器,删除标签 [ARTICLE] 和 [/ARTICLE] 之间的所有内容(当然包括标签本身)和这些部分我使用我的函数所做的特殊格式。
请注意,在我的自定义标签 TEXT 标签中,我有特殊的 MARK 示例:标签[TEXT] blah blah blah [[MARK]]这是强调文本[[/MARK]] [/TEXT]。为简单起见,因为 MARK 是唯一可以嵌套在 [TEXT] 内的标签,所以我将其设为 [[MARK]](带双括号]
如何按原样输出所有文本字段,[ARTICLE] 标记之间的部分除外,并将它们视为发送到自定义函数的参数?
非常感谢您的帮助!
【问题讨论】:
-
您为什么要构建自己的模板引擎?对于这类事情,有很多预制系统,PHP 本身就是一个模板引擎。
标签: php html mysql regex replace