检测断线&;在PHP中调用输出上的新段落


Detecting a line break & invoking a new paragraph on the output in PHP

所以我有这个片段,它从名为"cdet"&打开"index.php"&查找字符串"details"&用我的输入替换它-

if(ISSET($_REQUEST["sub"])){
    $cdet=$_REQUEST["cdet"];
    $fname = "index.php";
    $fhandle = fopen($fname,"r");
    $content = fread($fhandle,filesize($fname));
    $content = str_replace("details", $cdet, $content);
    $fhandle = fopen($fname,"w");
    fwrite($fhandle,$content); 
}
fclose($fhandle);

&这是"index.php"中字符串"details"所在的部分-

<p class="wNote">details</p>

我想要的是,如果输入中出现换行/换行,我将结束当前

&为新行调用一个新的。。。

例如-如果输入是

你好。。你在这里干什么?

那么细节应该被替换,比如-

<p class="wNote">Hello there..</p>
<p class="wNote">What are you doing here?</p>

首先,您可以使用file_get_contents()函数以更简单的方式加载文件:

http://php.net/manual/en/function.file-get-contents.php

然后,在获得$cdet字段值后,使用explode()函数通过"''n"号(新行)将其拆分。这样,您将得到一个包含文本行的数组。

然后遍历该数组(使用foreach()),并为每一行添加"<p class="wNote">",然后添加行内容,然后添加"</p>"。

最后,您不能只将"details"字替换为结果,但必须将整个"<p class="wNote">details</p>"替换为输出,因为现在可以有多行。