dsrw29618 2015-02-19 08:05
浏览 51
已采纳

如何将字符串中的每个单词存储到特定变量中?

Please help me to put these words of string into some specific variables.

$remove4 = "First 1st, Second 2nd, Third 3rd, Fourth 4th"; 

i'm using this code, but i can get it right. can somebody tell me what is the problem in my code?

    $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];
}

Goal:

Process1 = 'First';
Process2 = 'Second';
Process3 = 'Third';
Process4 = 'Fourth';

Temp1 = '1st';
Temp2 = '2nd';
Temp3 = '3rd';
Temp4 = '4th';
  • 写回答

2条回答 默认 最新

  • douchuanchai2793 2015-02-19 08:25
    关注
    $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 /> ";
    

    Output :

    First
    Second
    Third
    Fourth
    1st
    2nd
    3rd
    4th
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 win10权限管理,限制普通用户使用删除功能
  • ¥15 minnio内存占用过大,内存没被回收(Windows环境)
  • ¥65 抖音咸鱼付款链接转码支付宝
  • ¥15 ubuntu22.04上安装ursim-3.15.8.106339遇到的问题
  • ¥15 求螺旋焊缝的图像处理
  • ¥15 blast算法(相关搜索:数据库)
  • ¥15 请问有人会紧聚焦相关的matlab知识嘛?
  • ¥15 网络通信安全解决方案
  • ¥50 yalmip+Gurobi
  • ¥20 win10修改放大文本以及缩放与布局后蓝屏无法正常进入桌面