2011-06-03 139 views
5

我有一個包含UUID v4PHP的preg_match UUID V4

$uuid = 'http://domain.com/images/123/b85066fc-248f-4ea9-b13d-0858dbf4efc1_small.jpg'; 

我怎麼會得到從上面使用preg_match()b85066fc-248f-4ea9-b13d-0858dbf4efc1值的字符串? UUID的V4
更多信息,可以發現here

+0

不知道這個簡單的模式,但試圖「^(\ {{0,1}([0-9A-FA-F]){8} - ([0 -9a-FA-F]){4} - ([0-9A-FA-F]){4} - ([0-9A-FA-F]){4} - ([0-9A-FA- F]){12} \} {0,1})$「 – Rufinus 2011-06-03 05:13:30

+0

同樣,Rufinus,這是錯誤的,請參閱http://en.wikipedia.org/wiki/Universally_unique_identifier#Version_4_.28random.29 – Artefact2 2011-06-03 05:14:50

回答

11
$uuid = 'http://domain.com/images/123/b85066fc-248f-4ea9-b13d-0858dbf4efc1_small.jpg'; 
preg_match('!/images/\d+/([a-z0-9\-]*)_!i', $uuid, $m); 

而且

preg_match('/[a-f0-9]{8}\-[a-f0-9]{4}\-4[a-f0-9]{3}\-(8|9|a|b)[a-f0-9]{3‌​}\-[a-f0-9]{12}/', $uuid, $m); 

工作過。採取從here,但我不知道我們是否可以依靠這一點。

+0

不,這個正則表達式太寬容了。 – Artefact2 2011-06-03 05:15:18

+0

問題是如何從類似鏈接獲得'uuid',但不能從任何地方獲得,例如而不是來自文本。 UUID的定義是[這裏](http://en.wikipedia.org/wiki/Uuid#Definition),所以我想我的第二個解決方案實際上是被盜的,可以用於這個目的。 – Nemoden 2011-06-03 05:20:48

+12

更嚴格的正則表達式可以是/ [a-f0-9] {8} \ - [a-f0-9] {4} \ - 4 [a-f0-9] {3} \ - (8 | 9 | a | f0-9] {3} \ - [a-f0-9] {12} /' – 2014-07-13 22:40:28

5

你可以嘗試的UUID

preg_match('/\w{8}-\w{4}-\w{4}-\w{4}-\w{12}/',$uuid,$matches);