2013-09-26 6 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에서 작동하지만, 내 사이트에 내가 텍스트의 마지막에 공백 (새 회선)가 :

를 나는 공간 (새 라인)보다 "U"modificator를 사용합니다 경우 t의 상단에있을 것

test1 without U

흥미, 그는 텍스트 : 텍스트 영역의

test2 with U

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

화면 :

in the textarea

갱신이 (예)에 대한 아주 기본적인 페이지, u는 바로 [PHP]의 BBCode를 넣을 수 있습니다 텍스트 영역에 양식을 작성하여 제출하십시오.

<!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] my php code [/ php] 테스트 테스트," "'$ 출력 ="테스트, 테스트 테스트,

my php code
테스트 test, test ";'look, 나는'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

태그를 제거하지 않고 'pre' 태그로 [php] bbcode 만 만들고 있습니다. – Smash

+0

괜찮 았나? –

관련 문제