php字符串替换没有得到所需

时间:2015-08-24 06:09:30

标签: php

我有一个字符串s:

«math xmlns=¨http://www.w3.org/1998/Math/MathML¨»«msup»«mi»x«/mi»«mn»2«/mn»«/msup»«/math»

我想将其转换为:

<math><msup><mi>x</mi><mn>2</mn></msup></math>

我尝试的内容如下:

$text = str_replace("«math xmlns=¨http://www.w3.org/1998/Math/MathML¨»","<math>", $text);
$text = str_replace("«/math»","</math>", $text);
$text = str_replace("»Â",">", $text);
$text = str_replace("«","<", $text);

echo $text;

但是我的运气不好,我得到的输出字符串为:

«math xmlns=¨http://www.w3.org/1998/Math/MathML¨»«msup»«mi»x«/mi»«mn»2«/mn»«/msup»«/math»

我该怎么做?

1 个答案:

答案 0 :(得分:0)

只有几个str_replace要做......

$text = "«math xmlns=¨http://www.w3.org/1998/Math/MathML¨»«msup»«mi»x«/mi»«mn»2«/mn»«/msup»«/math»";

$text = str_replace("«math xmlns=¨http://www.w3.org/1998/Math/MathML¨»","<math>", $text);
$text = str_replace("«/math»","</math>", $text);
$text = str_replace("»Â",">", $text);
$text = str_replace("»",">", $text);
$text = str_replace("«","<", $text);
$text = str_replace("«","<", $text);
$text = str_replace("Â","", $text);

echo $text; // outputs <math><msup><mi>x</mi><mn>2</mn></msup></math>