开发者

How do I use preg_replace to return part of a string?

I am trying to return mime types from an array. I have these strings in an array:

application/pdf
image/jpg
image/jpeg

What I want to end up with is:

pdf
j开发者_开发百科pg
jpeg


Don't use preg replace. For each string in the array...

$split = explode('/', $mime);
$type = $split[1];


I want to show the allowable filetypes to the user

This may work fine in your specific case - I can't judge that - but in general, it would be cleaner to have a separate array/column/whatever for the file extensions, e.g.

array(
    "application/pdf" => "pdf",
    "image/jpg" => "jpg",
    "image/jpeg" => "jpeg"
);

Consider application/msword (.doc) or video/msvideo (.avi) where MIME type and file extension have nothing to do with each other. Your desired method of using the last part of the MIME type would break there.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜