我一直在努力奋斗数小时。 尝试过在线搜索解决方案,但我发现的一切都不是我想要的。我绝望地希望得到答案。
我有一个长字符串(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]标记之间的部分,并将它们作为参数发送到自定义函数?
非常感谢帮助!
答案 0 :(得分: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>";
}