Php 根据扩展名重定向url

Php 根据扩展名重定向url,php,Php,问题是,我试图重定向它是否是一个pdf到一个空白的目标,以及它是否是一个mp3音频到一个目标iframe,但我无法使它工作。它还有一个额外的字符串,它被带到已经包含href的数据库中,并且必须根据所提到的需要进行修改 function url($texto) { $cadena_resultante= preg_replace("/((http|https|www)[^\s]+)/", '<a href="$1">$0</a>', $texto); $ca

问题是,我试图重定向它是否是一个pdf到一个空白的目标,以及它是否是一个mp3音频到一个目标iframe,但我无法使它工作。它还有一个额外的字符串,它被带到已经包含href的数据库中,并且必须根据所提到的需要进行修改

function url($texto)
{
    $cadena_resultante= preg_replace("/((http|https|www)[^\s]+)/", '<a href="$1">$0</a>', $texto);
    $cadena_resultante= preg_replace("/href=\"www/", 'href="http://www', $cadena_resultante);
    ##Verificamos la extencion
    $trozos = explode(".", $cadena_resultante);
    //$extension = end($trozos);

    foreach($trozos as $b)
    {


        if(preg_match('/^mp3/',$b)) {
            $cambio = str_replace('<a href', '<a target="audio" href ', $cadena_resultante);
            echo $cambio;
        }
        elseif(preg_match('/^pdf/',$b))
        {
            $cambio = str_replace('<a target="audio" href="audio.php?titulo=http://localhost/audio/archivos/file/"', '<a target="_blank" href=http://localhost/audio/archivos/file/ ', $cadena_resultante);
        }

    }
    return $cambio;

}

您不需要使用regexp。对于DOM操作,有一个类用于此操作。选中此项:

$text = '<a href="pdf.pdf">pdf</a> texto mas texto por el texto <a href="something.mp3">mp3</a> texto texto mas texto texto texto';
function showHtml($text) {
    $Dom = new DOMDocument;
    $Dom->loadHTML($text);
    $links = $Dom->getElementsByTagName('a');
    foreach ($links as $link) {
        $href = $link->getAttribute('href');
        if (!empty($href)) {
            $pathinfo = pathinfo($href);
            if (strtolower($pathinfo['extension']) === 'mp3') {
                $link->setAttribute('target', "iframeId");
            } elseif (strtolower($pathinfo['extension']) === 'pdf') {
                $link->setAttribute('target', "_blank");
            }
        }
    }
    $html = $Dom->saveHTML();
    return $html;
}
showHtml($text);

什么是$texto。这是文件的URL吗?请给我一个可能的字符串。例如。。你好,谢谢,谢谢