簡體   English   中英

PHP - 選擇兩個字符之間的字符串的一部分

[英]PHP - Select part of a string between two characters

我一直在研究這個問題,我相信答案是用正則表達式做的,但我無法理解它們。

我有很多字符串,我需要選擇兩個字符之間的數字。 這是一個示例字符串

&user18339=18339,20070103,175439,pmt,793,A/3/1/2,335,793,A/3/1/2,

我需要在A/3/1/2, 3/1/2之后和之后發生的數字,

在這個例子中,我需要選擇335 我可以使用explode來做到這一點但是當我需要從字符串中獲取多個數字時遇到問題,如下例所示。

這是另一個示例字符串

&user31097=31097,20070105,092612,pmt,4190,A/3/1/2,142,1162,A/3/1/1,22,2874,A/3/1/2,1046,4622,A/3/1/2,25,2872,A/3/1/2,

再次,我需要后得到的數字A/3/1/2,和之前以下, 因此,在這個例子中,我要把這些142104625

如果有人能讓我知道如何做到這一點,將不勝感激。

$string = '&user31097=31097,20070105,092612,pmt,4190,A/3/1/2,142,1162,A/3/1/1,22,2874,A/3/1/2,1046,4622,A/3/1/2,25,2872,A/3/1/2,';
preg_match_all('/A\/3\/1\/2,([0-9]*?),/', $string, $matches);
var_dump($matches);
preg_match_all('/A\/3\/1\/2,([^,]+),/', $input, $matches = array());
print_r($matches);
if(preg_match_all('#A/3/1/2,([^,]*),#',$str,$matches)) {                        
        // $matches[1] will have the required results.
}

看到它在行動

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM