PHP - 如何从特定案例中获取数字作为数组
PHP - How to get number from specific case as array
这是我的字符串:
$string = 'This the s number [:[123084]:] and [:[123085]:] 12374 [:[123087]:]';
如果号码是[:[xxxx]:]这种情况,如何获取号码?我想从这个案例中获取数字(xxxx)作为数组。
我想要这样的结果:
$array = array( 123084, 123085, 123087);
这样就可以了:
$string = 'This the s number [:[123084]:] and [:[123085]:] 12374[:[123087]:]';
preg_match_all('/(?<=\[:\[)\d+(?=\]:\])/', $string, $matches);
print_r($matches);
输出:
Array
(
[0] => Array
(
[0] => 123084
[1] => 123085
[2] => 123087
)
)
解释:
/ : regex delimiter
(?<= : lookbehind, zero length assertion, make sure we have the following BEFORE the current position
\[:\[ : literally [:[
) : end lookbehind
\d+ : 1 or more digits
(?= : lookahead, zero length assertion, make sure we have the following AFTER the current position
\]:\] : literally ]:]
) : end lookahead
/ : regex delimiter
您会找到有用的信息here
这是我的字符串:
$string = 'This the s number [:[123084]:] and [:[123085]:] 12374 [:[123087]:]';
如果号码是[:[xxxx]:]这种情况,如何获取号码?我想从这个案例中获取数字(xxxx)作为数组。
我想要这样的结果:
$array = array( 123084, 123085, 123087);
这样就可以了:
$string = 'This the s number [:[123084]:] and [:[123085]:] 12374[:[123087]:]';
preg_match_all('/(?<=\[:\[)\d+(?=\]:\])/', $string, $matches);
print_r($matches);
输出:
Array
(
[0] => Array
(
[0] => 123084
[1] => 123085
[2] => 123087
)
)
解释:
/ : regex delimiter
(?<= : lookbehind, zero length assertion, make sure we have the following BEFORE the current position
\[:\[ : literally [:[
) : end lookbehind
\d+ : 1 or more digits
(?= : lookahead, zero length assertion, make sure we have the following AFTER the current position
\]:\] : literally ]:]
) : end lookahead
/ : regex delimiter
您会找到有用的信息here