我有两个字符串
$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;
但它只替换一个。