[英]PHP - Select part of a string between two characters
I've been doing research into this and I believe the answer is do to with regular expressions but I just can't get my head around them. 我一直在研究这个问题,我相信答案是用正则表达式做的,但我无法理解它们。
I have a number of strings and I need to select a number between two characters. 我有很多字符串,我需要选择两个字符之间的数字。 Here is an example string
这是一个示例字符串
&user18339=18339,20070103,175439,pmt,793,A/3/1/2,335,793,A/3/1/2,
I need the number that occurs after A/3/1/2,
and before the following ,
我需要在
A/3/1/2,
3/1/2之后和之后发生的数字,
In this example I need to select 335
. 在这个例子中,我需要选择
335
。 I can do this using explode however I run into problems when I need to get more than one number from a string, like in the example below. 我可以使用explode来做到这一点但是当我需要从字符串中获取多个数字时遇到问题,如下例所示。
Here is another example 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,
Again I need to get the numbers after the A/3/1/2,
and before the following ,
. 再次,我需要后得到的数字
A/3/1/2,
和之前以下,
。 So in this example I would want to take 142
, 1046
and 25
. 因此,在这个例子中,我要把这些
142
, 1046
和25
。
If anyone could let me know how to do this it would be greatly appreciated. 如果有人能让我知道如何做到这一点,将不胜感激。
$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.