【问题标题】:Parse Text field with custom Tags and convert to HTML使用自定义标签解析文本字段并转换为 HTML
【发布时间】: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


【解决方案1】:

我认为你想做的事情是完全可能的。可能有几种方法可以给这只猫剥皮,但我会这样做。

<?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>";



}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-01-11
    • 2012-05-26
    • 2023-04-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-04-04
    相关资源
    最近更新 更多