Pomoc - Szukaj - Użytkownicy - Kalendarz
Pełna wersja: Automatyczne tworzenie linków
Forum PHP.pl > Forum > PHP
8-ball
Witam,
jak zrobic automatyczne tworzenie linkow al'a phpbb3 bo jakos nie moge nic wymyslic;
adres http://www.twojadomena.pl/index.php badz bez http:// albo www. aby zmienil sie w <a href="adres">adres</a>
pomozecie?
marcio
Kiedys tez kombinowalem dzialac dziala ale nie do konca oto i funkcja:
  1. <?php
  2. function bbcode_autolink($str) {
  3.  
  4. $lnk=$str[0];
  5. if(strlen($lnk)>30) {
  6. if(substr($lnk,0,6)=='http://' || substr($lnk, 0, 2) == 'www') {
  7. $l = 15;
  8. }
  9. else {
  10. $l=15;
  11. }
  12. $lnk=substr($lnk,0,$l).'(...)'.substr($lnk,strlen($lnk)-8);
  13. }
  14. return ' <a href="'.$str[0].'" target="_blank">'.$lnk.'</a>';
  15. }
  16.  
  17. //BBcode
  18.  
  19. function bbcode($zmien) {
  20. $zmien = htmlspecialchars(trim($zmien));
  21. $zmien = preg_replace_callback("#(http[s]?://|ftp://|www.)([^ ]+)#", "bbcode_autolink", $zmien);
  22. return $zmien;
  23. }
  24. ?>
MMX3
Poniżej wstawiam co robi jeszcze tiny urle
  1. <?php
  2. class link_catcher{
  3.    var $message;
  4.    function link_catcher($string) {
  5.        $array = $this->catch_link($string);
  6.        if(count($array)>0) {
  7.                    for($i=0; $i<count($array); $i++) {
  8.                            if(strlen($array[$i][0])<link_lenght_limit){
  9.                                    $array[$i][1] = $array[$i][0];
  10.                            }else if(strlen($array[$i][0])<255){
  11.                                    $url = $array[$i][0];
  12.                                    $db = new database;
  13.                    $db->dblink();
  14.                    $tiny = $this->generate_tiny(5);
  15.                    if(substr($url,0,4)!="http")
  16.                            $url = "http://".$url;
  17.                    $rec = $db->get_rec("tiny_url", "tiny, url, id", "url = '{$url}'");
  18.                    if($rec->url == $url) {
  19.                        $array[$i][1] = root_domain."/url/".$rec->tiny;
  20.                        //updating an expire date
  21.                        $db->db_update("tiny_url","expires = {$expires}","id = {$rec->id}");
  22.  
  23.                    }else{
  24.                        $expires = time()+tiny_url_expires;
  25.                        if ($rec = $db->get_rec("tiny_url", "id,tiny, expires", "tiny = '{$tiny}'")) {
  26.                            if($rec->expires > time()) {
  27.                                //update an existing record
  28.                                $db->db_update("tiny_url","url = '{$url}', expires = {$expires}","id = {$rec->id}");
  29.                            }else{
  30.                                //generating another tiny
  31.                                $tiny = $this->generate_tiny(5);
  32.                            }
  33.  
  34.                        }else{
  35.                            $id = $db->db_insert("tiny_url", "url, tiny, author_id, expires","'{$url}', '{$tiny}', {$_SESSION['user']->id}, {$expires} ");
  36.                            $i=0;
  37.                            while(!$id&&$i<100) {
  38.                                $tiny = $this->generate_tiny(5);
  39.                                $id = $db->db_insert("tiny_url", "url, tiny, author_id, expires","'{$url}', '{$tiny}', {$_SESSION['user']->id}, {$expires} ");
  40.                                $i++;
  41.                            }
  42.                        }
  43.  
  44.                        
  45.                        $tiny_url = root_domain."/url/".$tiny ;
  46.                        $array[$i][1] = $tiny_url;
  47.                        
  48.                    }
  49.                    
  50.                                    
  51.                }else{
  52.                    //urls witch are longer than 255 will be cut to the main domain.
  53.                    $short = preg_split("/[/,]+/", $array[$i][0]);
  54.                    $domain = "http://".$short[1]."/";
  55.                    $array[$i][1] = $domain;
  56.                    
  57.                }
  58.                        //echo($string);
  59.                    $string = str_replace($array[$i][0], $array[$i][1], $string);
  60.            //    echo("robie {$i} tiny urla z: {$array[$i][0]} na: {$array[$i][1]}<br />{$string}");
  61.  
  62.            }
  63.            
  64.        }
  65.        
  66.                  
  67.        $this->message = urlencode(addslashes($string));
  68.        //echo $this->message;
  69.        /*echo("<pre>");
  70.         print_r($array);
  71.         echo("</pre>");*/
  72.    }
  73. ?>


Reszta kodu bo się nie zmieściło
  1. <?php
  2. function make_seed() {
  3.            list($usec,$sec) = explode(" ", microtime());
  4.            return ((float)$sec+(float)$usec) * 100000;
  5.    }
  6.    function make_clickable($string) {
  7.        if(substr($string,0,4)!="www.")
  8.                $res = "<a href=\"".$string."\" target=\"_blank\">".$string."</a>";
  9.        else
  10.            $res = "<a href=\"http://".$string."\" target=\"_blank\">".$string."</a>";
  11.        return $res;
  12.    }
  13.    function generate_tiny($length) {
  14.            mt_srand($this->make_seed());
  15.            $possible_characters = "abcdefghijkmnopqrstuvwxyz1234567890";
  16.            $string = "";
  17.            while(strlen($string)<$length) {
  18.                    $string .= substr($possible_characters, mt_rand()%(strlen($possible_characters)),1);
  19.            }
  20.            return $string;
  21.    }
  22.    function catch_link($string) {
  23.            
  24.        preg_match_all("/(ftp://|http://|https://|www.|ftp.)(w+:{0,1}w*@)?(S+)(:[0-9]+)?(/|/([w#!:.?+=&%@!-/]))?/", $string, $array, PREG_SET_ORDER);
  25.        $result = $array;
  26.        return $result;
  27.  
  28.    }
  29.  
  30.  
  31. };
  32. ?>

catch_link właściwie robi wszystko.
To jest wersja lo-fi głównej zawartości. Aby zobaczyć pełną wersję z większą zawartością, obrazkami i formatowaniem proszę kliknij tutaj.
Invision Power Board © 2001-2025 Invision Power Services, Inc.