php - 允许用户在 PHP 中使用一些静态值创建自定义段落
问题描述
让我们说这是静态字符串:
$name = "John";
$age = 37;
现在,假设用户在 Textarea 中输入以下值:
His name is {name}. His age is {age}
他还可以键入:
{name} is his name. {age} is his age.
现在,当用户提交表单时,PHP 应该将这些字符串设置到那些特定位置。
例如输出应该是这样的:
His name is John. His age is 37
John is his name. 37 is his age.
解决方案
好的,由于您没有提供问题的具体细节,我只能推测您可能想要这个:
<?php
$name = "John";
$age = 37;
$userInput1 = "His name is {name}. His age is {age}";
$userInput2 = "{name} is his name. {age} is his age.";
$result1 = str_replace("{name}", $name, $userInput1);
$result1 = str_replace("{age}", $age, $result1);
$result2 = str_replace("{name}", $name, $userInput2);
$result2 = str_replace("{age}", $age, $result2);
echo "$result1<br>$result2";
这只是一个非常简单的文本替换。
按照你说的,让用户从textarea输入,然后传给php,输出结果。这是一个演示:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<form action="a.php" method="post">
<textarea name="text_from_textarea"></textarea>
<button type="submit">Submit</button>
</form>
</body>
</html>
<?php
$name = "John";
$age = 37;
$userInput = $_POST["text_from_textarea"];
$result = str_replace("{name}", $name, $userInput);
$result = str_replace("{age}", $age, $result);
echo "$result";
推荐阅读
- php - Laravel hasMany 来自同一张表的先决条件
- java - 按下删除按钮时如何获取相应的名称
- python - 尝试使用 Spinbox 和 RadioButtons 将列表的一部分添加到另一部分
- c# - C# wpf 如何将变量 Excel.Worksheet 从 Button_Click 传递到另一个 Button_Click
- path - 关于图论中路径定义的困惑
- express - 如何不在 expressjs 上监听 3000 端口?
- java - 如何将多部分文件转换为java中的文件
- javascript - 当绘制 2 个甜甜圈时,D3JS 图形甜甜圈未显示一些楔形
- sql-server - 在 SQL Server 中筛选多行
- postgresql - 使用 md5 密码登录 postgresql