2012-02-12 5 views
0

링크를 생성하기 위해 TinySong API를 사용하고 있습니다. 이제 작동하기 때문에 linkify를 사용해 보았습니다. 그렇지 않았습니다. 왜 내가 올바른 변수를 사용했다고 생각하지 않는지 확실하지 않습니다. 여기에 코드가 있습니다.Linkify PHP 텍스트

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




} 


?> 

어떻게 결과 링크를 클릭 할 수있게합니까? 나는 올바른 코드를 사용하고 있습니까? 감사합니다

+0

다른 사람을 도움이 될 것입니다. 개인적으로, 나는 잠재적 인 문제를 찾기 위해 코드를 선별 해 내고자하는 욕망이나 성향을 갖고 있지 않다. 특별히 작동하지 않는 것을 알아 내고 그에 대해 물어보기 위해 사전 디버깅을 수행하십시오. – rdlowrey

답변

0

이이 사이트에서 잘 작동 나는이를 위해 ...

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; 
    } 
} 
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:[email protected]$3\">[email protected]$3</a>", $text); 
    return($text); 
} 

희망이 당신은 당신이 당신의 코드 대신 코드 블록에 전체 스크립트에 어떤 금액 덤프의 특정 문제 영역을 분리하면 긍정적 인 도움을받을 가능성이 높아