【问题标题】:Linkify PHP text链接 PHP 文本
【发布时间】:2012-02-12 16:00:34
【问题描述】:

我正在使用 TinySong api 生成链接,它可以工作,现在我尝试使用 linkify。它没有。我不确定为什么它没有链接我相信我使用了正确的变量。这是代码。

<?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        = '';
    }




}


?>

如何使结果链接可点击?我什至使用正确的代码?谢谢

【问题讨论】:

  • 如果您在代码中隔离特定问题区域而不是将相当于整个脚本的内容转储到代码块中,您更有可能获得积极的帮助。就个人而言,我没有愿望或倾向于逐行筛选您的代码以寻找潜在问题。进行一些预调试以找出具体的问题并询问。

标签: php api linkify


【解决方案1】:

这是我发现的唯一一个与 www 合作的。

function link_it($text)
{
    $text= preg_replace("/(^|[\n ])([\w]*?)((ht|f)tp(s)?:\/\/[\w]+[^ \,\"\n\r\t<]*)/is", "$1$2<a     href=\"$3\" >$3</a>", $text);
    $text= preg_replace("/(^|[\n ])([\w]*?)((www|ftp)\.[^ \,\"\t\n\r<]*)/is", "$1$2<a href=\"http://$3\" >$3</a>", $text);
    $text= preg_replace("/(^|[\n ])([a-z0-9&\-_\.]+?)@([\w\-]+\.([\w\-\.]+)+)/i", "$1<a href=\"mailto:$2@$3\">$2@$3</a>", $text);
    return($text);
}

希望这对其他人有所帮助

【讨论】:

    【解决方案2】:

    这在我使用它的网站上运行良好......

    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;
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-01-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-03-31
      相关资源
      最近更新 更多