如何使用正则表达式在下划线之间选择数字


How to pick numbers between underlines using regex?

我只想得到粗体显示的值,但是我没有得到。

349141

_ 194419414 _4828414_n.jpg

https://hphotos-ash3.net/t1.0-9/1146_54482593153_1214114_n.jpg

Thank you already

您可以使用preg_match和一个捕获组来获得结果:

<?php
    $searchText = "349141_194419414_4828414_n.jpg";
    $result = preg_match("/_(''d+)_/u", $searchText, $matches);
    print_r($matches[1]);
?>
输出:

194419414

(我不确定这是否是一个好方法,但你可以得到你想要的任何值)

$r="349141_194419414_4828414_n";
print_r(explode('_',$r));
输出:

Array ( [0] => 349141 [1] => 194419414 [2] => 4828414 [3] => n )

$rr=explode('_',$r);
echo $rr[1];

输出
194419414

试试这样:

.+/'d+_('d+)_'d+_n.jpg

下面是正则表达式的答案

$filename = '349141_194419414_4828414_n.jpg';
preg_match_all('/[0-9]+/', $filename, $matches);
echo $matches[0][1]; //194419414