2015-04-04 205 views
1

更換引號我有以下字符串:與打開和關閉的HTML標籤

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

但是,由於兩端都是不可行的標誌,最好的解決方法是什麼?

+0

您的字符串包含 ''也在兩個地方。你只是通過把它們放在''中來強調這兩個部分。而且總是你的字符串就像這樣或者單詞和句子會改變嗎? – 2015-04-04 15:26:57

回答

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> 
//^^^      ^^^^^^       ^^^^^     ^^^^^^ 
相關問題