【发布时间】:2023-04-02 20:10:01
【问题描述】:
我有两个字符串
$xml = '<para aid:pstyle="NL_FIRST">—To the best of our knowledge, the MMSN<emph aid:cstyle="ITALIC"> protocol</emph> is the first multifrequency<emph aid:cstyle="ITALIC"> MAC protocol especially</emph> designed for WSNs, in which each device is equipped with a single radio transceiver and the MAC layer packet size is very small.</para></item><item>';
$tex = '\begin{itemize}\item o the best of our knowledge, the MMSN protocol is the first multifrequency MAC protocol especially designed for WSNs, in which each device is equipped with a single radio transceiver and the MAC layer packet size is very small.\item';
我需要找到<emph aid:cstyle="ITALIC"> protocol</emph>这种标签,并在$tex中找到相同的文字并将"protocol"这个词替换为{it protocol }。
简单
我需要找到这个模式
<emph aid:cstyle="ITALIC"> protocol</emph>
并找到该模式中的文本并替换 $tex 中的相同单词。
仅供参考:在内容方面两者都是相同的 $tex 和 $xml。
我用过这段代码
preg_match_all('/<emph aid:cstyle="ITALIC">(.*?)<\/emph>(.*?)\</',$xml,$matches);
for($i=0;$i<count($matches[1]);$i++)
{
$findtext = str_replace("<","",$matches[1][$i].$matches[2][$i]);
$replace = "{\it".$matches[1][$i]."}".$matches[2][$i];
$finaltext = preg_replace('/'.$findtext.'/',$replace,$tex);
}
echo $finaltext;
但它只替换一个。
【问题讨论】:
-
您的问题是什么?请帮我找到一个不符合条件的解决方案。