dry0106
2017-07-22 13:41
浏览 91
已采纳

从包含3个以逗号分隔的数字的字符串中提取数字

I have this variable: $pattern = "73,1,72"; //string in quotes

I want to create 3 new variables, first one should contain first value, second the second, and the third should contain the third.

So eventually should have these values:

$var1 == 73
$var2 == 1
$var3 == 72

How can I extract those numbers?

图片转代码服务由CSDN问答提供 功能建议

我有这个变量: $ pattern =“73,1,72”; / / string in quotes

我想创建3个新变量,第一个应该包含第一个值,第二个应该包含第二个,第三个应该包含第三个。

所以最终应该有这些值:

  $ var1 == 73 
 $ var2 == 1 
 $ var3 == 72 
  <  / pre> 
 
 

如何提取这些数字?

  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

3条回答 默认 最新

  • dqwh1209 2017-07-22 13:50
    已采纳

    Use list() and explode() functions to achieve the desired result.

    list($var1, $var2, $var3) = explode(",", $pattern);
    

    Here's the demo, https://eval.in/836182

    打赏 评论
  • dongqixuan3112 2017-07-22 13:49
    $parts= explode(",", $pattern);
    $var1 = $parts[0];
    $var2 = $parts[1];
    $var3 = $parts[2];
    
    打赏 评论
  • doupixian1436 2017-07-22 13:52
    <?php
    $pattern = "73,1,72"; 
    $strings = explode(",",$pattern));
    $ints = array();
    for($i=0;$<count($strings);$i++)
    {
       array_push($ints,intval($strings[$i]));
    
    }
     $var1 =$ints[0];
     $var2 =$ints[1];
     $var3 =$ints[2];
    
    ?>
    
    打赏 评论

相关推荐 更多相似问题