0

我有一个问题,我认为你们可以帮助我解决这个问题。

我正在制作一个自定义博客,我在互联网上发现了一个非常酷的 php 文件,它提供了这些功能

function _make_url_clickable_cb($matches) {
    $ret = '';
    $url = $matches[2];

    if ( empty($url) )
        return $matches[0];
    // removed trailing [.,;:] from URL
    if ( in_array(substr($url, -1), array('.', ',', ';', ':')) === true ) {
        $ret = substr($url, -1);
        $url = substr($url, 0, strlen($url)-1);
    }
    return $matches[1] . "<a href=\"$url\" rel=\"nofollow\">$url</a>" . $ret;
}

我希望有一个功能可以自动检测发布的链接何时是图像(jpg、gif、png 等)并自动添加 img 标签。

我试着改变这个,但我不太擅长正则表达式

function _make_img_tag($matches) {
$ret = '';
$url = $matches[2];

if ( empty($url) )
    return $matches[0];
if ( in_array(substr($url, -1), array('.', ',', ';', ':')) === true ) {
    $ret = substr($url, -1);
    $url = substr($url, 0, strlen($url)-1);
}
return $matches[1] . "<img src=\"$url\" width=\"800\" height=\"600\">$url" . $ret;
}

接着

$ret = preg_replace_callback('#^(gif|png|jpg|jpeg)#is', '_make_img_tag', $ret);

但由于已添加其他替换回调,它仅显示为链接。

你们能帮帮我吗?

提前致谢,

编辑:发布完整代码

function _make_url_clickable_cb($matches) {
$ret = '';
$url = $matches[2];

if ( empty($url) )
    return $matches[0];
// removed trailing [.,;:] from URL
if ( in_array(substr($url, -1), array('.', ',', ';', ':')) === true ) {
    $ret = substr($url, -1);
    $url = substr($url, 0, strlen($url)-1);
}
return $matches[1] . "<a href=\"$url\" rel=\"nofollow\">$url</a>" . $ret;
}

function _make_web_ftp_clickable_cb($matches) {
$ret = '';
$dest = $matches[2];
$dest = 'http://' . $dest;

if ( empty($dest) )
    return $matches[0];
// removed trailing [,;:] from URL
if ( in_array(substr($dest, -1), array('.', ',', ';', ':')) === true ) {
    $ret = substr($dest, -1);
    $dest = substr($dest, 0, strlen($dest)-1);
}
return $matches[1] . "<a href=\"$dest\" rel=\"nofollow\">$dest</a>" . $ret;
}

function _make_email_clickable_cb($matches) {
$email = $matches[2] . '@' . $matches[3];
return $matches[1] . "<a href=\"mailto:$email\">$email</a>";
}

function _make_img_tag($matches) {
$ret = '';
$url = $matches[2];

if ( empty($url) )
    return $matches[0];
if ( in_array(substr($url, -1), array('.', ',', ';', ':')) === true ) {
    $ret = substr($url, -1);
    $url = substr($url, 0, strlen($url)-1);
}
return $matches[1] . "<img src=\"$url\" width=\"800\" height=\"600\">$url" . $ret;
}

function make_clickable($ret) {
$ret = ' ' . $ret;
$ret = preg_replace_callback('#^(gif|png|jpg|jpeg)#is', '_make_img_tag', $ret);
$ret = preg_replace_callback('#([\s>])([\w]+?://[\w\\x80-\\xff\#$%&~/.\-;:=,?@\[\]+]*)#is', '_make_url_clickable_cb', $ret);
$ret = preg_replace_callback('#([\s>])((www|ftp)\.[\w\\x80-\\xff\#$%&~/.\-;:=,?@\[\]+]*)#is', '_make_web_ftp_clickable_cb', $ret);
$ret = preg_replace_callback('#([\s>])([.0-9a-z_+-]+)@(([0-9a-z-]+\.)+[0-9a-z]{2,})#i', '_make_email_clickable_cb', $ret);

$ret = preg_replace("#(<a( [^>]+?>|>))<a [^>]+?>([^>]+?)</a></a>#i", "$1$3</a>", $ret);
$ret = trim($ret);
return $ret;
}
4

1 回答 1

2

您不需要新的正则表达式来查找图像。这两行已经找到它们:

$ret = preg_replace_callback('#([\s>])([\w]+?://[\w\\x80-\\xff\#$%&~/.\-;:=,?@\[\]+]*)#is', '_make_url_clickable_cb', $ret);
$ret = preg_replace_callback('#([\s>])((www|ftp)\.[\w\\x80-\\xff\#$%&~/.\-;:=,?@\[\]+]*)#is', '_make_web_ftp_clickable_cb', $ret);

您需要在进行替换的函数中将常规 URL 与图像区分开来。代替

return $matches[1] . "<a href=\"$url\" rel=\"nofollow\">$url</a>" . $ret;

经过

if (preg_match('#\.(gif|png|jpg|jpeg)$#is', $url))
    return $matches[1] . "<img src=\"$url\" width=\"800\" height=\"600\">$url" . $ret;
else
    return $matches[1] . "<a href=\"$url\" rel=\"nofollow\">$url</a>" . $ret;
于 2013-08-25T13:41:16.160 回答