2012-08-30 91 views
0

我在看一些代碼,具有這個pregmatch是如何工作的?

preg_match('/\[youtube ([[:print:]]+)\]/', $content, $matches) 

$content可能是一個鏈接,如* HTTP://www.youtube.com/watch V = some_video *

我可以看到它的過濾youtube視頻,但我不明白它是如何做的。更具體地說,[:print:]的作用是什麼?

+0

您是否閱讀過['re_format(7)'](http://www.freebsd.org/cgi/man.cgi?query=re_format)手冊頁以查看字符類如何工作? – ghoti

+0

老實說,這是我第一次瞭解角色類。感謝您的鏈接。 – KalenGi

回答

1

這裏的箱圖中的實驗。

/\[youtube ([[:print:]]+)\]/ 
│ │  │ │   │ 
│ │  │ │   └─ close the matched string 
│ │  │ └──────────── start the character class 
│ │  └────────────── open the matched string 
│ └─────────────────────── literal square bracket 
└───────────────────────── start the regexp 

重要的是圓括號內的部分。這與您的編程語言相匹配,以便作爲變量重用,以便您可以構建替換網址。