如何找到一部分字符串并将其回显?

时间:2014-02-09 21:07:43

标签: php

$string = "David Smith (Local/202@from-queue/n from SIP/202) (ringinuse enabled) (Not in use) has taken 1 calls (last was 843 secs ago)";

我需要回应所有调用(1)

我试过了:

$taken_calls = substr($string, 'taken ', 4 );

我该怎么做?三江源。

2 个答案:

答案 0 :(得分:0)

使用正则表达式:

<?php
$string = "David Smith (Local/202@from-queue/n from SIP/202) (ringinuse enabled) (Not in use) has taken 1 calls (last was 843 secs ago)";
$matches = array();

if (1 === preg_match('/taken (\d+) calls/', $string, $matches)) {
    echo $matches[1];
}

答案 1 :(得分:0)

$string = "David Smith (Local/202@from-queue/n from SIP/202) (ringinuse enabled) (Not in use) has taken 1 calls (last was 843 secs ago)";
$result= array();
preg_match("/taken\s(\d+)/", $string, $result);

var_dump($result);

结果:

array(2) { 
           [0]=> string(7) "taken 1" 
           [1]=> string(1) "1"    // this is your number 
}

编辑:现在,该数字可能超过1位数