我必须处理植物的拉丁文名称,并需要在来自DB的植物名称的部分字的样式.这些名称以原始文本的形式存储.
示例字符串:Androsace angrenica 'Angelica' subsp. Violaceae
预期成绩:<em>Androsace angrenica</em> 'Angelica' subsp. <em>Violaceae</em>
一些特定的单词将被排除在斜体标签中,如上面的例子和数组$toFind
中所示.
到目前为止,我得到了每个单词,但数组中的一个单词被<em></em>
包住了,其他单词除外,如下所示:
<em>Androsace</em> <em>angrenica</em> 'Angelica' subsp. <em>Violaceae</em>
我希望能够防止像名称的第一部分那样跟随</em> <em>
,并将它们加入到第一个示例中所示的单个标记环绕中.
# Array of words not be wraped in italic
$toFind = ["subsp.", "var.", "f.", "(voir)", "hybride"];
# Plant name
$name = "Androsace angrenica 'Angelica' subsp. Violaceae";
# Make an array of words from the name
$words = explode( " ", $name );
$newWords = [];
foreach( $words as $key => $word ) {
if( in_array( $word, $toFind )) {
$newWords[] = $word;
}else{
# Catch the word or words surrounded by single quotes like 'Angelica'
$isHybrid = preg_match_all( "/'([^.]*?)'/", $word, $matches, PREG_PATTERN_ORDER );
if( $isHybrid ){
# No tags required
$newWords[] = $word ;
}else{
# Tags required for these words
$newWords[] = "<em> ". $word . "</em>";
}
}
}
echo implode(" ", $newWords);
请注意,此示例名称是许多可能的名称之一,如下所示:
Allium obliquum
-
Allium ostrowkianum (voir) A. oreophilum
个 Allium senescens subsp. glaucum
-
Allium sikkimense
个 -
Androsace × pedemontana
个