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>

它适用于沙盒测试,但在我的网站上,文本末尾有一个空格(新行):

没有 U 的 test1

有趣的是,如果我使用“U”修饰符,那么空格(新行)将位于文本的顶部:

测试2与U

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

文本区域的屏幕:

在文本区域

为此更新一个非常基本的页面(示例),您只需将 [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>

您会看到底部或顶部的空间。

4

1 回答 1

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
于 2013-09-26T09:42:38.153 回答