因此,我希望用PHP编写一个函数getType()。该函数将从web表单(在CodeIgniter中)获取用户输入的字符串,然后从中分析其内容,然后确定该字符串是照片(以.jpg、.png等结尾)、youtube视频链接、vimeo视频链接还是仅文本。
我只是很难想象最好、最经济的方法。
if (strpos($content, ".jpg|.png|.bmp"))
{ return "image"; }
else if (strpos($content, "youtube.com"))
{ return "youtube"; }
else if (strpos($content, "vimeo.com"))
{ return "vimeo" }
else
{ return "text" }
这应该有效:
// check if string ends with image extension
if (preg_match('/('.jpg|'.png|'.bmp)$/', $content)) {
return "image";
// check if there is youtube.com in string
} elseif (strpos($content, "youtube.com") !== false) {
return "youtube";
// check if there is vimeo.com in string
} elseif (strpos($content, "vimeo.com") !== false) {
return "vimeo";
} else {
return "text";
}
演示:http://codepad.viper-7.com/1V4joK
请注意,不能保证它是youtube或vimeo链接。因为这只检查字符串是否与服务匹配,而不检查其他内容。