简体   繁体   English

使用php代码在textarea中格式化文本

[英]Formatting text inside a textarea using php code

I have 2 textareas. 我有2个textareas。 In the first textarea the user adds text which i need to modify. 在第一个文本区域中,用户添加我需要修改的文本。 In the 2nd textarea the amended text is displayed. 在第二个文本区域中显示修改后的文本。

Text that I need to modify has the following format: 我需要修改的文本具有以下格式:

23.10.15
Text1
Text2

I want to put text on the same line. 我想在同一行上输入文字。 Between the 2nd and 3rd line I want "-" to appear, like this: 我希望在第二行和第三行之间出现“-”,如下所示:

23.10.15 Text1 - Text2

My code looks like this: 我的代码如下所示:

if(isset($_POST["submit"])) {
 $text = $_POST["text-modify"];
 $text = str_replace('', '', $text);
}
    ?>

<form id="form1" name="form1" method="post" action="">

<input type="submit" name="submit" id="submit" value="Submit" />

<textarea name="text-modify" id="text-modify" cols="75" rows="7000">
</textarea>

<textarea name="text-final" id="text-final" cols="75" rows="7000">
<?php echo $text; ?>
</textarea>
</form>

Thank you in advance. 先感谢您。

Explode the text on newlines, pop off the last item, implode with spaces, append the dash and the last piece: 在换行符上展开文本,弹出最后一项,内插空格,并在横线和最后一段附加:

$pieces = explode("\n", $text);
$last = array_pop($pieces);
$text = implode(" ", $pieces) . " - " . $last;

Even better could be: 更好的可能是:

$text = call_user_func_array('sprintf', array_merge(["%s %s - %s"], explode("\n", $text)); 

You can put your php in the html to make it simpler like such : 您可以将您的php放入html中,使其变得更简单,例如:

<textarea name="text-modify" id="text-modify" cols="75" rows="7000"><?php $yourVar ?></textarea>

or embed with a string 或嵌入一个字符串

<textarea name="text-modify" id="text-modify" cols="75" rows="7000"><?php echo "Hi I am ". $nameVar ." How are you ? "</textarea>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM