preg_replace更改括号和文本

时间:2014-02-25 10:40:58

标签: latex brackets preg-replace-callback

我在PHP中有一个文本字符串:

$str = 'This is a simple \^{text}. It does not look well \_{styled}.';

我想要做的是使用preg_replace更改“\ ^ {text}”和“_ {styled}”来获取这样的HTML代码

$str = 'This is a simple <sup>text<sup>. It does not look well <sub>styled</sub>.';

我现在所做的是:

function Translate($str)
{
   return preg_replace('/\\\\\^\{.*\}/i', '<sup>$1</sup>', $str);
}
echo Translate("this offer starts at \^{abc}");

这与$ 1无法正常工作!在$ 0整场比赛是正确的... 有人可以帮助我吗? Regars!

1 个答案:

答案 0 :(得分:0)

解决方案:快速而肮脏:$ str ='这是一个简单的\ ^ {text}。它看起来不太好{{styled}。'; echo preg_replace('〜(\\\ ^ {)(。?)(})〜',' $ 2 ',$ str); echo preg_replace('〜(\\ _ {)(。?)(})〜',' $ 2 ',$ str);也许有人有更好的解决方案?

相关问题