Zac*_*ner 1 php function preg-replace
我正在编写一个函数,它从 中获取内容$_POST,将其插入到一个字符串中,然后返回结果字符串
对于“你最喜欢的颜色是什么?”这个问题。用户输入blue
问题“你最喜欢的动物是什么?” 用户输入dog
$content = "The visitor's favorite color is {color}";
$content = sentenceBuilder($content);
$content = "The visitor's favorite animal is a {animal}";
$content = sentenceBuilder($content);
function sentenceBuilder($content){
global $_POST;
foreach($_POST as $key => $value){
if($key=='color'){
$content = preg_replace("/\{($key+)\}/", $value, $content);
}
if($key=='animal'){
$content = preg_replace("/\{($key+)\}/", $value, $content);
}
}
return $content;
}
Run Code Online (Sandbox Code Playgroud)
这将返回“访客最喜欢的颜色是蓝色”和“访客最喜欢的动物是狗”。如果他们将颜色元素留空,则返回“访客最喜欢的颜色是”和“访客最喜欢的动物是狗”。如果他们将两个元素都留空,则返回 2 个不完整的句子。
所以,我试图修改它说如果$value是空的,该函数将跳过它并移动到下一个(因为它使用在 $_POST 中移动的每个表单元素)......
if($key=='color' && $value!=''){
$content = preg_replace("/\{($key+)\}/", $value, $content);
}else{
$content ='';
}
if($key=='animal' && $value!=''){
$content = preg_replace("/\{($key+)\}/", $value, $content);
}else{
$content ='';
}
Run Code Online (Sandbox Code Playgroud)
加上这个,我得到的结果是空白的。没有句子什么的。即使他们填写了元素,添加了此代码的结果仍然是空白的。
所以我尝试了这个。
function sentenceBuilder($content){
global $_POST;
foreach($_POST as $key => $value){
if(isset($value) && $value!=''){
if($key=='color'){
$content = preg_replace("/\{($key+)\}/", $value, $content);
}
if($key=='animal'){
$content = preg_replace("/\{($key+)\}/", $value, $content);
}
else{
$content = '';
}
}
return $content;
}
Run Code Online (Sandbox Code Playgroud)
这产生了相同的结果。
TLDR;
我希望能够让这个函数用一个句子替换不为空的值的内容。那些是空的,我希望不显示内容。
更新
我得到了工作的代码。不得不重新设计它以实现它。
<?php
if(isset($_POST)){
$content = "The visitor's favorite color is {color}";
echo sentenceBuilder($content);
?> <br/> <?php
$content = "The visitor's favorite animal is a {animal} from {land}";
echo sentenceBuilder($content);
?> <br/> <?php
$content = "The visitor is from {land}";
echo sentenceBuilder($content);
?> <br/> <?php
$content = "The visitor is from Iowa";
echo sentenceBuilder($content);
?> <br/> <?php
$content = "The visitor is from {state}";
echo sentenceBuilder($content);
}
function sentenceBuilder($content){
preg_match("/\{(.*?)\}/", $content, $checkbrackets);
if(!empty($checkbrackets)){
$gettext = str_replace('{', '', $checkbrackets[0]);
$gettext = str_replace('}', '', $gettext);
if(array_key_exists($gettext,$_POST)){
if(!empty($_POST[$gettext])){
$content = preg_replace("/\{($gettext+)\}/", $_POST[$gettext], $content);
}else{
$content = '';
}
}
}
return $content;
}
?>
<form method="post" action"sentencebuilder.php">
<input type="text" name="color" />
<input type="text" name="animal" />
<input type="text" name="land" />
<input type="submit" />
</form>
Run Code Online (Sandbox Code Playgroud)
感谢您的帮助伙计们。输入它,你会看到我要做什么。我目前正在努力更改问题中存在的其他括号。
使用此代码,绝对是您问题的解决方案:-
<?php
$main_content = array();
if(isset($_POST['submit'])) {
unset($_POST['submit']);
$main_content['color'] = "The visitor's favorite color is {color}";
$main_content['dog'] = "The visitor's favorite dog is {dog}";
$main_content['school'] = "The visitor's favorite school is {school}";
$formData = array_filter($_POST);
if(!empty($formData)) {
echo sentenceBuilder($formData, $main_content);
}
}
function sentenceBuilder($formData=null, $main_content=null){
$newContent = "";
foreach ($formData as $key => $value) {
$newVal = "";
$newVal = preg_replace("/\{($key+)\}/", $value, $main_content[$key]);
$newContent .= $newVal.". <br/>";
}
return $newContent;
}
?>
<form method="post" action="#">
<input type="text" name="color" placeholder="color" />
<input type="text" name="dog" placeholder="dog" />
<input type="text" name="school" placeholder="school" />
<input type="submit" name="submit" />
**OUTPUT:**
The visitor's favorite color is RED.
The visitor's favorite dog is BULLDOG.
The visitor's favorite school is CAMPUS SCHOOL.
Run Code Online (Sandbox Code Playgroud)