2013-09-26 27 views
0

我的字符串是:如何從字符串中刪除空格(新行)?

$str = "[php] 

complete 
once 
A lot of text and other staffs! 
but 

[/php]"; 

$str = preg_replace("/\[php\]\s*(.*?)\s*\[\/php]/is","<pre>\\1</pre>",$str); 

echo $str; 

上了:

<pre>complete 
once 
A lot of text and other staffs! 
but</pre> 

它可以在沙箱中test,但在我的網站我在文字後面加上一個空格(新線):

test1 without U

有趣的是,如果我將使用「U」變質比空間(新線)將在T的頂部他的文字:

test2 with U

$str = preg_replace("/\[php\]\s*(.*?)\s*\[\/php]/Uis","<pre>\\1</pre>",$str); 

屏幕文字區域:

in the textarea

更新此(例如)一個非常基本的網頁,U可以只是把[PHP]的bbcode在textarea的表格上提交。

<!DOCTYPE html> 
<html lang="en"> 
<head> 
<meta charset="UTF-8" /> 
<style> 
.code { 
    border: 1px solid #c2c2c2; 
    padding: 4px; 
    border-radius: 4px; 
} 
</style> 
</head> 
<body> 
<?php 
$str = $_POST['textarea']; 
$content = preg_replace("/\[php\]\s*(.*)\s*\[\/php\]/si","<pre class='code'>\\1</pre>",$str); 
echo $content; 
?> 
<form method="post" action="" class="contactform"> 
<textarea name="textarea"></textarea> 
<input type="submit" class="met_button pull-right" name="submit" value="Send"> 
</form> 
</body> 
</html> 

您會在底部或頂部看到空格。

+0

什麼是輸入,電流輸出和期望的輸出? – Halcyon

+0

那麼,輸入可以是不同的,例如:'$ input_str =「test,test test,[php]我的php代碼[/ php] test test,test」;''''output''test,test test,

my php code
test測試,測試「;'看,我不需要'pre'標籤中的空格。其他字符串文本必須是原樣。 – Smash

+0

我要求你澄清。如果你不能澄清(或不想),我不能給你一個解決方案。 – Halcyon

回答

0

試試這個:

$str = "[php]  

complete 
once 
A lot of text and other staffs! 
but 


[/php]"; 

$arr = array('[php]','[/php]'); 

$string = str_replace($arr,'', $str); 
echo $string ; 

輸出:

complete 
once 
A lot of text and other staffs! 
but 
+0

我不需要所有的字符串,只在'pre'標籤中。 – Smash

+0

我並沒有移除標籤,只是將[php] bbcode製作成'pre'標籤。 – Smash

+0

好了,你明白了 –