php - function that cleans values passed through $_POST before it submits it -
php - function that cleans values passed through $_POST before it submits it -
i'm writing function takes content $_post
, inserts in string , returns resulting string
to question "what favorite color?" user inputs blue
question "what favorite animal?" user inputs dog
$content = "the visitor's favorite color {color}"; $content = sentencebuilder($content); $content = "the visitor's favorite animal {animal}"; $content = sentencebuilder($content); function sentencebuilder($content){ global $_post; foreach($_post $key => $value){ if($key=='color'){ $content = preg_replace("/\{($key+)\}/", $value, $content); } if($key=='animal'){ $content = preg_replace("/\{($key+)\}/", $value, $content); } } homecoming $content; }
this returns "the visitor's favorite color blue" , "the visitor's favorite animal dog." if leave color element blank, returns "the visitor's favorite color " , "the visitor's favorite animal dog". if leave both elements blank, returns 2 incomplete sentences.
so, tried modified if $value
empty, function skip , move on next (as uses every form element moved on in $_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 =''; }
with added, result blank. no sentences or anything. if fill out elements, result still blank code added.
so tried instead.
function sentencebuilder($content){ global $_post; foreach($_post $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 = ''; } } homecoming $content; }
this yielded same results.
tldr;
i want able have function replace content values not empty sentence. ones empty, content not displayed.
update
i got code work. had redesign create happen though.
<?php if(isset($_post)){ $content = "the visitor's favorite color {color}"; echo sentencebuilder($content); ?> <br/> <?php $content = "the visitor's favorite animal {animal} {land}"; echo sentencebuilder($content); ?> <br/> <?php $content = "the visitor {land}"; echo sentencebuilder($content); ?> <br/> <?php $content = "the visitor iowa"; echo sentencebuilder($content); ?> <br/> <?php $content = "the visitor {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 = ''; } } } homecoming $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>
thanks help guys. come in in , you'll see going for. working on alter additional brackets exist within question.
use code, that's solution of problem:-
<?php $main_content = array(); if(isset($_post['submit'])) { unset($_post['submit']); $main_content['color'] = "the visitor's favorite color {color}"; $main_content['dog'] = "the visitor's favorite dog {dog}"; $main_content['school'] = "the visitor's favorite school {school}"; $formdata = array_filter($_post); if(!empty($formdata)) { echo sentencebuilder($formdata, $main_content); } } function sentencebuilder($formdata=null, $main_content=null){ $newcontent = ""; foreach ($formdata $key => $value) { $newval = ""; $newval = preg_replace("/\{($key+)\}/", $value, $main_content[$key]); $newcontent .= $newval.". <br/>"; } homecoming $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:** visitor's favorite color red. visitor's favorite dog bulldog. visitor's favorite school campus school.
php function preg-replace
Comments
Post a Comment