从“:”和“,”之间的字符串中获取值
Get Value from string between ":" and ","
这是我的字符串示例
"{"id":128,"order":128,"active":"1","name":"\"
现在我需要获取“128”- id 参数。所以它的第一个值在“:”和“,”之间。
我试过 preg_match 和不同的正则表达式,但我只是不擅长正则表达式。也许有人会知道怎么做?
$id = preg_match('/:(,*?)\,/s', $content, $matches);
<?php
$txt='"{"id":128,"order":128,"active":"1","name":"\"';
$re1='.*?'; # Non-greedy match on filler
$re2='(\d+)'; # Integer Number 1
$re3='.*?'; # Non-greedy match on filler
$re4='(\d+)'; # Integer Number 2
$re5='.*?'; # Non-greedy match on filler
$re6='(\d+)'; # Integer Number 3
if ($c=preg_match_all ("/".$re1.$re2.$re3.$re4.$re5.$re6."/is",$txt, $matches))
{
$int1=$matches[1][0];
$int2=$matches[2][0];
$int3=$matches[3][0];
print "($int1) ($int2) ($int3) \n";
}
?>
这是使用正则表达式获取第一个 :
之后的数字的示例代码:
$re = "/(?<=\:)[0-9]+/";
$str = "\"{\"id\":128,\"order\":128,\"active\":\"1\",\"name\":\"\"";
preg_match($re, $str, $matches);
print $matches[0];
这是 TutorialsPoint 上的示例程序。
关于这个正则表达式的一个小细节 (?<=\:)[0-9]+
:幸运的是,它使用固定宽度 look-behind that PHP supports。
这是我的字符串示例
"{"id":128,"order":128,"active":"1","name":"\"
现在我需要获取“128”- id 参数。所以它的第一个值在“:”和“,”之间。
我试过 preg_match 和不同的正则表达式,但我只是不擅长正则表达式。也许有人会知道怎么做?
$id = preg_match('/:(,*?)\,/s', $content, $matches);
<?php
$txt='"{"id":128,"order":128,"active":"1","name":"\"';
$re1='.*?'; # Non-greedy match on filler
$re2='(\d+)'; # Integer Number 1
$re3='.*?'; # Non-greedy match on filler
$re4='(\d+)'; # Integer Number 2
$re5='.*?'; # Non-greedy match on filler
$re6='(\d+)'; # Integer Number 3
if ($c=preg_match_all ("/".$re1.$re2.$re3.$re4.$re5.$re6."/is",$txt, $matches))
{
$int1=$matches[1][0];
$int2=$matches[2][0];
$int3=$matches[3][0];
print "($int1) ($int2) ($int3) \n";
}
?>
这是使用正则表达式获取第一个 :
之后的数字的示例代码:
$re = "/(?<=\:)[0-9]+/";
$str = "\"{\"id\":128,\"order\":128,\"active\":\"1\",\"name\":\"\"";
preg_match($re, $str, $matches);
print $matches[0];
这是 TutorialsPoint 上的示例程序。
关于这个正则表达式的一个小细节 (?<=\:)[0-9]+
:幸运的是,它使用固定宽度 look-behind that PHP supports。