php str_replace 剥离文件扩展名

php str_replace strip file extension

我正在尝试在 php 中创建一个 str_replace,我将从数据库输出中删除任何类型的文件扩展名。

完整php脚本

<?php
/* YOU CAN PLACE YOUR CUSTOM FUNCTIONS IN HERE TO MAKE UPDATING EASIER. THEY WILL NOT BE OVERWRITTEN BY UPDATES */

//display video player
function displayPlayerHtml5($rrow, $width = 590, $height = 475,   $skipAutoPlay = false) {
global $domain;
global $licenseKeyHTML5;
global $basehttp;
global $video_url;
global $player_streamer;
global $encodeTrailer;
global $modsec_enabled;
global $adultCentroPassword;
global $player_watermark_url;
global $video_path;

if ($player_streamer == 'rtmp') {
    unset($video_url);
}
if (strpos($width, '%') === false) {
    $width = $width . 'px';
}
if (strpos($height, '%') === false) {
    $height = $height . 'px';
}

    if ($rrow['embed']) {
        echo resizeEmbedded($rrow['embed'], $width, $height);
    } else {
        if ($rrow['hotlinked']) {
            if (stripos($rrow['hotlinked'], 'adultcentro') !== false) {
                $defaultFile = $basehttp . '/index.php' . urlencode('?controller=adultCentroGetLink&id=' . str_replace('adultcentro', '', $rrow['hotlinked']) . '&hash=' . md5($adultCentroPassword . str_replace('adultcentro', '', $rrow['hotlinked']) . $_SERVER['REMOTE_ADDR']));
            } else {
                $defaultFile = $rrow['hotlinked'];
            }
        } else {
            $video_file = $rrow['filename'];
            $subdir = $rrow[filename][0] . '/' . $rrow[filename][1] . '/' . $rrow[filename][2] . '/' . $rrow[filename][3] . '/' . $rrow[filename][4] . '/';
            if ($encodeTrailer && $rrow['trailer_filename'] && !$_SESSION['userid']) {
                $rrow['filename'] = $rrow['trailer_filename'];
            }
            if ($modsec_enabled) {
                $defaultFile = getMediaLink($subdir . $rrow['filename']);
            } else {
                $defaultFile = $video_url . '/' . $subdir . $rrow['filename'];
                $FileWebmpath = $video_path . '/' . $subdir . str_replace(".mp4",".webm",$rrow['filename']);
                $FileFLVpath = $video_path . '/' . $subdir . str_replace(".mp4",".flv",$rrow['filename']);
            }
        }

        if (file_exists($FileWebmpath)) {
            $FileWebm = ($video_url . '/' . $subdir . str_replace(".mp4",".webm",$rrow['filename']));
        } else {}

        if (file_exists($FileFLVpath)) {
            $FileFLV = ($video_url . '/' . $subdir . str_replace(".mp4",".flv",$rrow['filename']));
        } else {}
        ?>

        <? $subtitle = ($video_path . '/' . $subdir . $rrow['filename']) . ('-da.vtt');
         if (file_exists($subtitle)) { 
            $subtitles = ($video_url . '/' . $subdir . $rrow['filename']) . ('-da.vtt');
            } else {
            }
        ?>

        <div id="with-subtitles"
            class="minimalist flowplayer play-button color-light"
            preload="metadata"
            data-swf="//<? echo $domain; ?>/includes/html5/flowplayer.swf"
            data-key="<? echo $licenseKeyHTML5; ?>"
            data-logo="<? echo $player_watermark_url; ?>"
            data-splash="true" 
            data-embed="false"
            data-fullscreen="true"
            data-poster="<? echo getThumbUrl($rrow['orig_filename'],$rrow['filename'],$rrow['main_thumb'],true); ?>"
            data-analytics="UA-31104891-3"
            >
            <video>
                <source type="video/mp4" src="<? echo $defaultFile; ?>">
                <source type="video/webm" src="<? echo $FileWebm; ?>">
                <source type="video/flash" src="<? echo $FileFLV; ?>">
                <track kind="subtitles" srclang="da" label="Dansk" src="<? echo $subtitles; ?>">
            </video>
         </div>
        <?

}

}

//display video player Admin Interface
function displayPlayerHtml5adm($rrow, $width = 450, $height = 300, $skipAutoPlay = false) {
global $domain;
global $licenseKeyHTML5;
global $basehttp;
global $video_url;
global $player_streamer;
global $encodeTrailer;
global $modsec_enabled;
global $adultCentroPassword;
global $player_watermark_url;

if ($player_streamer == 'rtmp') {
    unset($video_url);
}
if (strpos($width, '%') === false) {
    $width = $width . 'px';
}
if (strpos($height, '%') === false) {
    $height = $height . 'px';
}

    if ($rrow['embed']) {
        echo resizeEmbedded($rrow['embed'], $width, $height);
    } else {
        if ($rrow['hotlinked']) {
            if (stripos($rrow['hotlinked'], 'adultcentro') !== false) {
                $defaultFile = $basehttp . '/index.php' . urlencode('?controller=adultCentroGetLink&id=' . str_replace('adultcentro', '', $rrow['hotlinked']) . '&hash=' . md5($adultCentroPassword . str_replace('adultcentro', '', $rrow['hotlinked']) . $_SERVER['REMOTE_ADDR']));
            } else {
                $defaultFile = $rrow['hotlinked'];
            }
        } else {
            $video_file = $rrow['filename'];
            $subdir = $rrow[filename][0] . '/' . $rrow[filename][1] . '/' . $rrow[filename][2] . '/' . $rrow[filename][3] . '/' . $rrow[filename][4] . '/';
            if ($encodeTrailer && $rrow['trailer_filename'] && !$_SESSION['userid']) {
                $rrow['filename'] = $rrow['trailer_filename'];
            }
            if ($modsec_enabled) {
                $defaultFile = getMediaLink($subdir . $rrow['filename']);
            } else {
                $defaultFile = $video_url . '/' . $subdir . $rrow['filename'];
            }
        }
        ?>
        <link rel="stylesheet" type="text/css" href="//<? echo $domain; ?>/includes/html5/skin/minimalist.css">
        <script src="//code.jquery.com/jquery-1.10.2.min.js"></script>
        <script type="text/javascript" src="//<? echo $domain; ?>/includes/html5/flowplayer.min.js"></script>

        <div class="flowplayer play-button color-light"
            style="max-width: 450px; max-hight: 300px;"
            data-swf="//<? echo $domain; ?>/includes/html5/flowplayer.swf"
            preload="metadata"
            data-key="<? echo $licenseKeyHTML5; ?>"
            data-splash="true"
            data-fullscreen="true"
            data-embed="false"
            data-poster="<? echo getThumbUrl($rrow['orig_filename'],$rrow['filename'],$rrow['main_thumb'],true); ?>">
            <video>

                <source type="video/mp4" src="<? echo $defaultFile; ?>">
            </video>
         </div>
        <?

  }
}

?>

我希望它能替换任何 .flv .mp4 .webm 等。如果文件确实存在,则替换为 .webm 和 .flv 的固定值。因此必须进行文件检查。

我不能 manipulate/add 列到 db 做特色升级..

一些例子。来自 db 54b1d96b89943.flv 5547e6f7b2694Politijagtmedover200km_t-[720p].mp4

的文件名

有人可以帮我吗?

正如我从你的问题中了解到的。你可以试试这个

 $fileExtensions=array(original file extensions here);    
 $replacement=array(replacement extensions);    

然后

 if(file_exists($fileName)){    str_replace($fileExtensions, $replacement, $fileName);    
 }
while ( substr($filename,-1)!=".") ){
   $filename = substr($filname,0,-1); 
}
$filename = substr($filname,0,-1);

$newext=".flv";
$newfilename = $filename.$newext;