我昨天提交了一个问题,但我认为这个问题太长而且令人困惑。今天我缩短了所有内容并提交了我的代码示例。代码背后的想法是解析内容,将内容插入到一个表单中,该表单将在此示例中回显或插入到mysql数据库中。该代码没有错误,但只显示单个字母和数字而不是完整的单词,请参阅下面的详细信息:
<?php
$fullstring = "Name: Steve - Age: 25, Name: Bob - Age: 30, Name: Amanda - Age: 18,"; // Content to be parsed
function get_string_between($string, $start, $end) {
$start = preg_quote($start);
$end = preg_quote($end);
$pattern = "~$start\s*(.*?)$end\s*~";
$match = preg_match_all($pattern, $string, $matches);
if ($match) {
return $matches[1];
}
}
$parsed1 = get_string_between($fullstring, "Name: ", "-");
$parsed2 = get_string_between($fullstring, "Age: ", ",");
////////// The form //////////////////////
echo '<form action="" method="POST">';
for ($i=0; $i < count($parsed1); $i++) {
echo "Name: <input name=\"name\" type=\"text\" value=\"". $parsed1[$i]. "\">","<br>" ;
echo "Age type: <input name=\"age\" type=\"text\" value=\"" . strip_tags($parsed2[$i]) . "\">","<br>" ;
}
echo '<input type="submit" name="submit" value="submit">';
echo '</form>';
if (isset($_POST['submit'])) {
$i = 0;
foreach ($_POST as $value) {
$name = $_POST['name'][$i];
$age = $_POST['age'][$i];
echo $name.'....'.$age.'<br>';
$i++;
}
}
//// mysql_query("INSERT INTO users (name, age) VALUES ('$name', '$age')");
//////////////////////////////////////////////////////////////
?>
当我按提交时,我得到了回应:
A....1
m....8
a....
Instead of of all the names in the in the form.
感谢所有帮助,谢谢大家。
答案 0 :(得分:1)
您可以使用name[]
和age[]
echo '<form action="" method="POST">';
for ($i=0; $i < count($parsed1); $i++) {
echo "Name: <input name=\"name[]\" type=\"text\" value=\"". $parsed1[$i]. "\">","<br>" ;
echo "Age type: <input name=\"age[]\" type=\"text\" value=\"" . strip_tags($parsed2[$i]) . "\">","<br>" ;
}
echo '<input type="submit" name="submit" value="submit">';
echo '</form>';
if (isset($_POST['submit'])) {
foreach($_POST['name'] as $k=>$name){
echo $name.'.... Age:'. $_POST['age'][$k];
}
}
答案 1 :(得分:0)
你的问题在于以下几点:
$name = $_POST['name'][$i];
$age = $_POST['age'][$i];
您从每个$i
键获取索引$_POST
处的字符。只需删除其中每一行中的[$i]
,您的输出就会变为
阿玛.... 18
阿玛.... 18
请注意,没有必要在循环中使用它 - 由于foreach
,您实际上是在两次输出。
答案 2 :(得分:0)
试试这个:经过测试:
$fullstring = "Name: Steve - Age: 25, Name: Bob - Age: 30, Name: Amanda - Age: 18,"; // Content to be parsed
function get_string_between($string, $start, $end) {
$start = preg_quote($start);
$end = preg_quote($end);
$pattern = "~$start\s*(.*?)$end\s*~";
$match = preg_match_all($pattern, $string, $matches);
if ($match) {
return $matches[1];
}
}
$parsed1 = get_string_between($fullstring, "Name: ", "-");
$parsed2 = get_string_between($fullstring, "Age: ", ",");
echo '<form action="" method="POST">';
for ($i=0; $i < count($parsed1); $i++) {
echo "Name: <input name=\"name{$i}\" type=\"text\" value=\"". $parsed1[$i]. "\">","<br>" ;
echo "Age type: <input name=\"age{$i}\" type=\"text\" value=\"" . strip_tags($parsed2[$i]) . "\">","<br>" ;
}
echo '<input type="submit" name="submit" value="submit">';
echo '</form>';
if (isset($_POST['submit'])) {
$i = 0;
while ($i < count($parsed1)) {
$name = $_POST['name'.$i];
$age = $_POST['age'.$i];
echo $name.'....'.$age.'<br>';
$i++;
}
}