【问题标题】:analyse the content and extracting link and images in php分析内容并提取php中的链接和图像
【发布时间】:2011-07-06 10:16:25
【问题描述】:

您可能已经看到 facebook 如何分析内容并提取链接图像和视频

当我们插入这样的东西时

this is an example text which is linked linke this   http://stackoverflow.com/questions/ask
and i like it so much like this picture     http://stackoverflow.com/img.png

变成这个

    this is an example text which is linked linke this   
<a href='http://stackoverflow.com/questions/ask'>
    and i like it so much like this picture   
 <img src='http://stackoverflow.com/img.png'>

现在我想做同样的事情并分析我的内容并产生丰富的内容。

我怎样才能在 php 中做这样的事情?有没有准备好的课程?

【问题讨论】:

    标签: php html class filter


    【解决方案1】:

    这是两个可以正常工作的功能,经过我的测试。

    function isImage($url) {
        if (substr($url, 0, 7) == 'http://') {
            $i = str_ireplace('http://', '', $url); // delete first part
            $i = explode('/', $i); // divide link into parts
            $end = end($i); // get the last part
            $ex = explode('.', $end); // get the extension if exists so for 'image.jpg' $ex = jpg
            if (count($ex) === 2) {
                $allowed = array('jpg', 'jpeg', 'png', 'gif'); // allowed image extensions
                foreach ($allowed as $a) {
                    if ($ex = $a) {
                        # Is an image
                        return true;
                    }
                }
            } else {
                # It's an url
                return false;
            }
        } else {
            # It's not even a link
            return false;
        }
    }
    
    function isLink($url) {
        if (substr($url, 0, 7) == 'http://') {
            $i = str_ireplace('http://', '', $url); // delete first part
            $i = explode('/', $url); // divide link into parts
            $end = end($i); // get the last part
            $ex = explode('.', $end); // get the extension if exists so for 'image.jpg' $ex = jpg
            return (count($ex) == 0) ? true : false;
        } else {
            # It's not even a link
            return false;
        }
    }
    
    $image = isImage($input); // true
    $link = isLink($input); // false
    var_dump($image, $link);
    

    示例 (isLink()):

    • 'google.com' = false (*)
    • 'http://google.com' = true
    • 'http://google.com/image.png' = 假

    示例 (isImage()):

    • 'google.com' = 假
    • 'google.com/image.png' = false (*)
    • 'http://google.com/image.png' = true
    • 'http://google.com/view/model/controller' = false

    (*) = 这是因为它检查链接是否有 http:// 作为前缀。如果你不希望这种情况发生,那么下面是函数:

    function isImage($url) {
        $i = explode('/', $i); // divide link into parts
        $end = end($i); // get the last part
        $ex = explode('.', $end); // get the extension if exists so for 'image.jpg' $ex = jpg
        if (count($ex) === 2) {
            $allowed = array('jpg', 'jpeg', 'png', 'gif'); // allowed image extensions
            foreach ($allowed as $a) {
                if ($ex = $a) {
                    # Is an image
                    return true;
                }
            }
        } else {
            # It's an url
            return false;
        }
    }
    
    function isLink($url) {
        $i = explode('/', $url); // divide link into parts
        $end = end($i); // get the last part
        $ex = explode('.', $end); // get the extension if exists so for 'image.jpg' $ex = jpg
        return (count($ex) == 0) ? true : false;
    }
    

    然后您可以将其用作:

    if (isLink($url)) { echo "<a href=\"$url\">$url</a>"; }
    if (isImage($url)) { echo "<img src=\"$url\" alt=\"image\">"; }
    

    【讨论】:

    【解决方案2】:

    您可以使用 preg_replace 和适当的正则表达式来替换匹配的模式并添加 HTML 标记。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-05-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-04-04
      • 1970-01-01
      • 1970-01-01
      • 2019-03-06
      相关资源
      最近更新 更多