$string = "David Smith (Local/[email protected]/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);
我該怎麼辦呢?謝謝。
$string = "David Smith (Local/[email protected]/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);
我該怎麼辦呢?謝謝。
$string = "David Smith (Local/[email protected]/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個位數
謝謝,那有效。 – user3148002
如果數字超過1,preg_match是多少,有時數字可能是2位數字或3位數字。此刻如果數字是12,我只在數組字符串中獲得1。 – user3148002
嗯..是的,你是對的,我會編輯它:) – demonking
使用正則表達式:
<?php
$string = "David Smith (Local/[email protected]/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];
}
閱讀正則表達式的章節 –