Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/88.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 从textfield表单获取两个不同的数组_Php_Html_Arrays_Forms_Textfield - Fatal编程技术网

Php 从textfield表单获取两个不同的数组

Php 从textfield表单获取两个不同的数组,php,html,arrays,forms,textfield,Php,Html,Arrays,Forms,Textfield,我在html表单中有一个文本区域输入。 用户将在该文本区域中写入如下内容: 5x蓝色花朵 2红花 3*黄花 紫花 所以我需要从这里得到两个数组。一个是数字,另一个是花 现在我得到了数组中的数字,但我正努力在第二个数组中只得到花朵。 此外,如果他们没有放置数字,则应该有一个默认数字1 $text_data = $_POST['tekst']; $input = explode("\n", $text_data); foreach($input as $line) { $number = preg_

我在html表单中有一个文本区域输入。 用户将在该文本区域中写入如下内容:

5x蓝色花朵 2红花 3*黄花 紫花 所以我需要从这里得到两个数组。一个是数字,另一个是花

现在我得到了数组中的数字,但我正努力在第二个数组中只得到花朵。 此外,如果他们没有放置数字,则应该有一个默认数字1

$text_data = $_POST['tekst'];
$input = explode("\n", $text_data);
foreach($input as $line)
{
$number = preg_replace("/[^0-9]/", '', $line);
echo $number . '<br>';
echo $line;
}
任何帮助都将不胜感激。

试试看

foreach($input as $line){
 preg_match("/\d+/", $line, $matches);
 $line = preg_replace("/\d+/",'' ,$line);
 $number = (isset($matches[0]))?$matches[0]:1;
 if(strlen($line)>0){
   echo $number."-->".$line."\n";
 }
}

见演示

非常感谢!这正是我所需要的为什么我不能在那里使用foreach,就像$line不是数组一样