问题是我正在尝试将它是否是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;
}
你不需要
使用正则表达式。对于 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);