I submitted a question yesterday but I think it was too long and confusing. Today I shortened everything and submitting an example of my code. The idea behind the code is to parse content, insert the content into a form which will be either echoed in this example or inserted into mysql database. The code works with no errors but show only individual letter and digits instead of full words, please see details below:
<?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')");
//////////////////////////////////////////////////////////////
?>
When I press submit I get this echoed:
A....1
m....8
a....
Instead of of all the names in the in the form.
I appreciate all the help thank you all.
[$i]from loop, see what happends.