0

i have two strings

  $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';

I need to find <emph aid:cstyle="ITALIC"> protocol</emph> This kind of tag and find the same text in $tex and replace the word "protocol" with {it protocol } .

Simply

i need to find this pattern

<emph aid:cstyle="ITALIC"> protocol</emph>

and find the text inside that pattern and replace the same word in $tex.

FYI : Content-wise both are same $tex and $xml.

I used this code

  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;

But it replace only one.

1
  • 1
    What is your question? Please help me find a solution does not qualify as one. Commented May 30, 2012 at 20:33

1 Answer 1

1

You should change your regex to

preg_match_all('/<emph aid:cstyle="ITALIC">(.+?)<\/emph>/', $xml, $matches);

I tried it on your sample string and it finds both.

Your current regex consumes to much of the string. If you run it on the provided string you will see that it matches more than you intended it to match

string(71) "<emph aid:cstyle="ITALIC"> protocol</emph> is the first multifrequency<"

Since the next "<" is already matched the matcher can not find the next opening tag.

For the loop-part: You are not overwriting $tex but you are using it as the string to work on. So any changes but the last will not be stored.

$finaltext = $tex;
for ($i = 0; $i <count($matches[1]); $i++) {
    $finaltext = str_replace($matches[1][$i], '{\it'.$matches[1][$i].'}', $finaltext);
}
echo $finaltext;
Sign up to request clarification or add additional context in comments.

3 Comments

i have changed the regular expression. But still it replace only one value.. any idea why it matches only one value.
In every iteration you work on the $tex-string which is never changed. You save your changes into $finaltext. Check the updated version, it should work. Plus I took the liberty to replace the regular expressions in the loop with simpler and faster str_replace.
@Dipen Baskaran: Did this solution help?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.