php - 使用自定义标签解析文本字段并转换为 HTML

标签 php html mysql regex replace

我已经为此苦苦挣扎了几个小时。 曾尝试在线搜索解决方案,但我发现的一切都不完全是我想要的。迫切希望得到答案。

我有一个包含文本的长字符串(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] 结尾的每个地方,我不会按原样输出,而是想调用我的自定义函数以不同方式设置格式。

例如:

函数 format_text_with_articles(ArtTitle、ArtDate、ArtBy、ArtText){ //这只是我已经拥有的一个简单函数 //params 和 formats 一个内部有特殊格式的表格 //文章摘录看起来不错。 }

因此,只需将所有文本输出到浏览器,删除标签 [ARTICLE] 和 [/ARTICLE] 之间的所有内容(当然包括标签本身)以及我使用我的函数执行的特殊格式处理的这些部分。

请注意,在我的自定义标签 TEXT 标签中,我有特殊的 MARK 示例:tags[TEXT] blah blah blah [[MARK]]这是强调文本[[/MARK]] [/TEXT]。为简单起见,因为 MARK 是唯一可以嵌套在 [TEXT] 中的标签,我将其作为 [[MARK]] (带双括号)

如何按原样输出所有文本字段,[ARTICLE] 标记之间的部分除外并将它们视为发送到自定义函数的参数?

非常感谢您的帮助!

最佳答案

我认为你想做的是完全可能的。可能有几种方法可以给这只猫剥皮,但这就是我要采用的方法。

<?php

$full_article = '[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]';



// MATCH ALL OF THE ARTICLE TAGS IN YOUR TEXT AND STORE EACH ONE INTO $matches
preg_match_all('~\[ARTICLE\](.*?)\[/ARTICLE\]~ms', $full_article, $matches);



// LOOP THROUGH EACH OF THE MATCHES, DO SOME FORMATTING AND REPLACE THE EXISTING CONTENT
for ($i = 0; $i < count($matches[1]); $i++) {

    $article = $matches[1][$i]; // TEXT WE WILL OPERATE ON
    $existing_article = $matches[0][$i]; // TEXT WE WILL BE REPLACING

    // PULL OUT EACH OF THE FIELDS WE WANT TO PASS ALONG TO OUR FUNCTION
    preg_match('~\[TITLE\](.*?)\[/TITLE\]~ms', $article, $match_article_title);
    preg_match('~\[DATE\](.*?)\[/DATE\]~ms', $article, $match_article_date);
    preg_match('~\[BY\](.*?)\[/BY\]~ms', $article, $match_article_by);
    preg_match('~\[TEXT\](.*?)\[/TEXT\]~ms', $article, $match_article_text);

    $article_title = $match_article_title[1];
    $article_date = $match_article_date[1];
    $article_by = $match_article_by[1];
    $article_text = $match_article_text[1];



    // SEND THE VARIABLES TO A FUNCTION TO FORMAT THE TEXT
    // THIS IS WHAT WE WILL BE REPLACING THE EXISTING TEXT WITH
    $replacement_text = format_text_with_articles ($article_title, $article_date, $article_by, $article_text);



    // REPLACE THE EXISTING ARTICLE TEXT WITH OUR REPLACEMENT TEXT    
    $full_article = preg_replace('/'.preg_quote($existing_article, '/').'/', $replacement_text, $full_article);

}



// PRINT OUT THE FINISHED ARTICLE
print $full_article;



// THIS FUNCTION TAKES SOME PARAMS AND PRETTIES THEM UP FOR THE DANCE    
function format_text_with_articles ($article_title, $article_date, $article_by, $article_text) {



    // REPLACE THE 'MARK' BRACES WITH BOLD TAGS        
    $article_text = preg_replace('~\[\[MARK\]\](.*?)\[\[/MARK\]\]~ms', '<b>$1</b>', $article_text);



    // RETURN THE FORMATTED TEXT BACK TO THE CALLING 'FOR' LOOP
    return "<span style=\"font-family: VERDANA; font-size: 11px;\"><p style=\"font-weight: bold; margin-top: 20px; margin-bottom: 5px;\">".$article_title."</p><p style=\"color: blue; margin: 0px;\">".$article_date."</p><p style=\"color: orange; margin: 0px;\">By: ".$article_by."</p><p style=\"margin-top: 5px; margin-bottom: 20px;\">".$article_text."</p></SPAN>";



}

关于php - 使用自定义标签解析文本字段并转换为 HTML,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22180067/

相关文章:

php - 这里 $get 命令有任何错误..没有显示错误,所以没有显示结果

php - MySql:根据时间戳和动态间隔选择数据

php 不从数据库获取图像位置

mysql - 对于初学者,MySQL 和 PostgreSQL 之间有很大区别吗?

php - 是否值得学习 C 来更深入地了解操作系统和计算机?

php - 使用 CakePHP 销毁 session

javascript - 如何从后端将变量传递到 $rootScope

html - div 不需要的填充底部

javascript - 编辑注册表单 woocommerce 中的字段类型

html - 覆盖特定 html 元素的 CSS 属性