2012-08-30 41 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 

重要的是圆括号内的部分。这与您的编程语言相匹配,以便作为变量重用,以便您可以构建替换网址。