Php 想要预匹配数字和周期吗
我有这个字符串Php 想要预匹配数字和周期吗,php,preg-match,Php,Preg Match,我有这个字符串&OrigPlacedDate=41759.7128&,我想将数字与句点匹配在正确的位置。我试过使用 $string = '&OrigPlacedDate=41759.7128&' $origPlacedDate = '/&OrigPlacedDate=[0-9^\.]*&/'; preg_match($origPlacedDate, $string, $origPlacedDateMatches); $origPlacedDate = preg_r
&OrigPlacedDate=41759.7128&
,我想将数字与句点匹配在正确的位置。我试过使用
$string = '&OrigPlacedDate=41759.7128&'
$origPlacedDate = '/&OrigPlacedDate=[0-9^\.]*&/';
preg_match($origPlacedDate, $string, $origPlacedDateMatches);
$origPlacedDate = preg_replace('/[^0-9]/', '', $origPlacedDateMatches);
print_r($origPlacedDate);
但我只知道数字
Array ( [0] => 417597128 )
最终,我希望得到41759.7128的输出。您匹配了数字,但没有在正则表达式中包含
。试试这个:
$string = '&OrigPlacedDate=41759.7128&';
$origPlacedDate = '/&OrigPlacedDate=[0-9^\.]*&/';
preg_match($origPlacedDate, $string, $origPlacedDateMatches);
$origPlacedDate = preg_replace('/[^0-9\.]/', '', $origPlacedDateMatches);
print_r($origPlacedDate);
输出:
Array
(
[0] => 41759.7128
)
您正在匹配数字,但未在正则表达式中包含
。试试这个:
$string = '&OrigPlacedDate=41759.7128&';
$origPlacedDate = '/&OrigPlacedDate=[0-9^\.]*&/';
preg_match($origPlacedDate, $string, $origPlacedDateMatches);
$origPlacedDate = preg_replace('/[^0-9\.]/', '', $origPlacedDateMatches);
print_r($origPlacedDate);
输出:
Array
(
[0] => 41759.7128
)
为什么不通过使用简单的正则表达式来使用更简单的方法呢
$string = '&OrigPlacedDate=41759.7128&';
$origPlacedDate = '/&OrigPlacedDate=([0-9]+\.[0-9]+)&/';
preg_match($origPlacedDate, $string, $origPlacedDateMatches);
echo $origPlacedDateMatches[1];
它输出:
41759.7128
为什么不通过使用简单的正则表达式来使用更简单的方法呢
$string = '&OrigPlacedDate=41759.7128&';
$origPlacedDate = '/&OrigPlacedDate=([0-9]+\.[0-9]+)&/';
preg_match($origPlacedDate, $string, $origPlacedDateMatches);
echo $origPlacedDateMatches[1];
它输出:
41759.7128
期望的输出是什么?我不明白用句号匹配数字意味着什么。你到底希望发生什么?你无论如何都无法得到那个数组-你没有捕获组(
()
),所以$origPlacedDateMatches中唯一的内容就是整个匹配的字符串。@MarcB我问这个问题时遗漏了一些代码,它还没有更新。@Marcus我问这个问题时遗漏了一些代码,它还没有更新。如果您想
,请不要用preg\u replace将它们删除<代码>[^0-9.]所需的输出是什么?我不明白用句号匹配数字意味着什么。你到底希望发生什么?你无论如何都无法得到那个数组-你没有捕获组(()
),所以$origPlacedDateMatches中唯一的内容就是整个匹配的字符串。@MarcB我问这个问题时遗漏了一些代码,它还没有更新。@Marcus我问这个问题时遗漏了一些代码,它还没有更新。如果您想
,请不要用preg\u replace将它们删除<代码>[^0-9.]