Linkify PHP文本 [英] Linkify PHP text

查看:120
本文介绍了Linkify PHP文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我现在用的是TinySong API生成一个链接,它的工作原理,现在我试图用一个linkify它。事实并非如此。我不知道为什么它不linkifing我相信我用正确的变量。这里是code。

I am using the TinySong api to generate a link, It works, now I tried using a linkify for it. It didn't. I'm not sure why it is not linkifing I believe I used the right variable. Here is the code.

<?php
  // linkify URLs
  $pre = preg_replace(
    '/(https?:\/\/\S+)/',
    '<a href="\1">\1</a>',
    $pre
  );
?>
 <script src="http://platform.twitter.com/anywhere.js?id= MY API KEY&v=1" type="text/javascript"></script>
<?php



class Tinysong
{
    protected $api_key = '';
    protected $method = '';
    protected $limit = '';
    protected $query_string = '';


    public static $CURL_OPTS = array(
        CURLOPT_CONNECTTIMEOUT => 10,
        CURLOPT_RETURNTRANSFER => true,
        CURLOPT_TIMEOUT        => 60,
        CURLOPT_USERAGENT      => 'tinysong-php-0.7',
    );


    public function __construct($api_key)
    {
        $this->api_key = $api_key;

    }




    /**
     * A wrapper for RESTful method /a/ (single 
     * @return @Tinysong
     */
    public function single_tinysong_link($query_string)
    {
        $this->query_string($query_string);
        return $this->method('a');
    }

       public function search($query_string)
    {
        $this->query_string($query_string);
        return $this->method('a');
    }

    /**
     * A wrapper for RESTful method /s/ (search)
     * @return Tinysong
     */


    /**
     * Sets the query string
     * @return Tinysong
     */
    public function query_string($query_string)
    {
        $this->query_string = urlencode($query_string);
        return $this;

 }

    /**
     *
     * @param type $method 
     * @return Tinysong
     */
    public function method($method)
    {
        $this->method = $method;
        return $this;
    }


    /**
     * Fetchs the data based on the parameters
     * @param bool $clean_params cleans the params after build the url
     * @param resource $ch a custom php curl resource
     * @return array an associative array with the data
     */
    public function execute($clean_params = true, $ch = null)
    {

        $url = $this->build_query();

        if ($clean_params)
        {
            $this->clean_params();
        }

        if (!$ch)
        {
            $ch = curl_init($url);
            curl_setopt_array($ch, self::$CURL_OPTS);
        }


        $query_result = curl_exec($ch);

        curl_close($ch);


        return  json_decode($query_result, true);

    }


    /**
     * Builds an API query based on the parameters
     * @return string the query
     */
    public function build_query()
    {
        $url = "http://tinysong.com";
        $url .= '/'.$this->method.'/';
        $url .= $this->query_string.'?';

        if ($this->limit)
        {
            $url .= 'limit='.$this->limit;
        }


        $url .= '&key='.$this->api_key;
        $url .= '&format=json';

        return $url;
    }


    /**
     * Cleans the params (method, query string and limit)
     * @return Tinysong
     */
    public function clean_params()
    {
        $this->method       = '';
        $this->query_string = '';
        $this->limit        = '';
    }




}


?>

如何让我的结果链接点击?难道我连使用正确的code?谢谢

How do I make the result link clickable? Am I even using the right code?? Thanks

推荐答案

这是对我使用它的网站运作良好...

This is working well on the sites I am using it for...

function find_urls($t){
    $reg_exUrl = "/(http|https|ftp|ftps)\:\/\/[a-zA-Z0-9\-\.]+\.[a-zA-Z]{2,3}(\/\S*)?/";
    // Check if there is a url in the text
    if(preg_match($reg_exUrl, $t, $url)) {
        $add='';
        if (substr($url[0],(strlen($url[0])-1),strlen($url[0]))==")"){
            $url[0]=substr($url[0],0,(strlen($url[0])-1));
            $add=')';
        } else if (substr($url[0],(strlen($url[0])-1),strlen($url[0]))=="]"){
            $url[0]=substr($url[0],0,(strlen($url[0])-1));
            $add=']';
        }
        // make the urls hyper links
        return preg_replace($reg_exUrl, '<a href="'.$url[0].'">'.$url[0].'</a>'.$add, $t);
    } else {
        // if no urls in the text just return the text
        return $t;
    }
}

这篇关于Linkify PHP文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆