用open和close html标签替换引号

时间:2015-04-04 15:21:39

标签: php string str-replace

我有以下字符串:

'something can go here' and there some more text 'then some more here'

我想成为:

<pre>something can go here</pre> and there some more text <pre>then some more here</pre>

我知道你可以这样做:

 str_replace($replace, $with, $from);

但是,由于两端都是不相同的符号,解决这个问题的最佳方法是什么?

1 个答案:

答案 0 :(得分:3)

这应该适合你:

(只需使用preg_replace(),然后您可以根据需要替换引号)

<?php

    $str = "'something can go here' and there some more text 'then some more here'";
    echo $str = preg_replace("/'(.*?)'/", "<pre>$1</pre>", $str);

?>

输出:

<pre>something can go here</pre> and there some more text <pre>then some more here</pre>
//^^^                     ^^^^^^                          ^^^^^                   ^^^^^^