2014-02-25 40 views
1

我在PHP中的文本字符串:的preg_replace变化括号和文本

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

我想要做的是改变“\^{文字}”和“_ {样式}”使用的preg_replace得到这样

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

HTML代码,我现在这样做是:

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

这并不正确以$ 1的工作!在$ 0整个比赛是正确的... 有人可以帮助我吗? Regars!

回答

0

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