如何将字符串中的每个单词存储到特定的变量中?
How to store each word in a string into specific variables?
请帮我把字符串的这些词放到一些特定的变量中。
$remove4 = "First 1st, Second 2nd, Third 3rd, Fourth 4th";
我正在使用这段代码,但我可以正确使用。有人可以告诉我我的代码有什么问题吗?
$str = (explode(",",$remove4));
$check = array();
for($i=0;$i<=$count;$i++){
$check[] = $str[$i];
}
foreach($check as $value){
echo $value . "<br>";
$var=(explode(" ", $value));
echo $var[0];
echo $var[1];
}
目标:
Process1 = 'First';
Process2 = 'Second';
Process3 = 'Third';
Process4 = 'Fourth';
Temp1 = '1st';
Temp2 = '2nd';
Temp3 = '3rd';
Temp4 = '4th';
你可以试试这个
foreach($check as $key => $value){
//echo $value . "<br>";
$var=(explode(" ", $value));
${'Process'.($key+1)} = $var[0];
${'Temp'.($key+1)} = $var[1];
}
echo $Process1;
echo $Temp3;
$remove4 = "First 1st, Second 2nd, Third 3rd, Fourth 4th";
$str = (explode(",",$remove4));
$check = array();
$i=0; foreach ($str as $value) { $i++;
$explode = (array_values(array_filter(explode(" ",$value))));
${'Process'.($i)} = $explode[0];
${'Temp'.($i)} = $explode[1];
}
echo $Process1 . "<br /> ";
echo $Process2 . "<br /> ";
echo $Process3 . "<br /> ";
echo $Process4 . "<br /> ";
echo $Temp1 . "<br /> ";
echo $Temp2 . "<br /> ";
echo $Temp3 . "<br /> ";
echo $Temp4 . "<br /> ";
输出:
First
Second
Third
Fourth
1st
2nd
3rd
4th
请帮我把字符串的这些词放到一些特定的变量中。
$remove4 = "First 1st, Second 2nd, Third 3rd, Fourth 4th";
我正在使用这段代码,但我可以正确使用。有人可以告诉我我的代码有什么问题吗?
$str = (explode(",",$remove4));
$check = array();
for($i=0;$i<=$count;$i++){
$check[] = $str[$i];
}
foreach($check as $value){
echo $value . "<br>";
$var=(explode(" ", $value));
echo $var[0];
echo $var[1];
}
目标:
Process1 = 'First';
Process2 = 'Second';
Process3 = 'Third';
Process4 = 'Fourth';
Temp1 = '1st';
Temp2 = '2nd';
Temp3 = '3rd';
Temp4 = '4th';
你可以试试这个
foreach($check as $key => $value){
//echo $value . "<br>";
$var=(explode(" ", $value));
${'Process'.($key+1)} = $var[0];
${'Temp'.($key+1)} = $var[1];
}
echo $Process1; echo $Temp3;
$remove4 = "First 1st, Second 2nd, Third 3rd, Fourth 4th";
$str = (explode(",",$remove4));
$check = array();
$i=0; foreach ($str as $value) { $i++;
$explode = (array_values(array_filter(explode(" ",$value))));
${'Process'.($i)} = $explode[0];
${'Temp'.($i)} = $explode[1];
}
echo $Process1 . "<br /> ";
echo $Process2 . "<br /> ";
echo $Process3 . "<br /> ";
echo $Process4 . "<br /> ";
echo $Temp1 . "<br /> ";
echo $Temp2 . "<br /> ";
echo $Temp3 . "<br /> ";
echo $Temp4 . "<br /> ";
输出:
First
Second
Third
Fourth
1st
2nd
3rd
4th