Urll BB Code

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    Urll BB Code

    PHP Code:
    function links_preg2($arr)
    {
        global 
    $set;
        if (
    preg_match('#^http://' preg_quote($_SERVER['HTTP_HOST']) . '#',$arr[2]))
        return 
    $arr[1] . '<a href="' $arr[2] . '">' $arr[2] . '</a>' $arr[4];
        else
        return 
    $arr[1] . '<a' . ($set['web'] ? ' target="_blank"' null) . ' href="http://' $_SERVER['HTTP_HOST'] . '/go.php?go=' base64_encode(html_entity_decode($arr[2])) . '">Link</a>' $arr[4];
    }

    function 
    links($msg)
    {
        global 
    $set;
        if (
    $set['bb_img'])$msg preg_replace_callback('/\[img\](.+)\[\/img\]/isU''img_preg'$msg);
        if (
    $set['bb_url'])$msg preg_replace_callback('/\[url=(.+)\](.+)\[\/url\]/isU''links_preg1'$msg);
        
        if (
    $set['bb_http'])$msg preg_replace_callback('~(^|\s)([a-z]+://([^ \r\n\t`\'"]+))(\s|$)~iu''links_preg2'$msg);
        
        return 
    $msg;

    This is the code that i have on my site for url link's. I want to make a new think:
    -each time when a user post a url: http://youtube.com/watch?blablabla , i want to show on the site : Link youtube
    -each time when a user post a url" http://www.facebook.com/picture/etcetcetctjkjk , i want to show on the site: Link facebook
    Anyone have an ideea how to make something like this? I gues that with preg_replace or url_parsing but i don't know how

    #2
    PHP Code:
    $str preg_replace('/http:\/\/(.*)./is', $1$arr[2]);
    // $str is now the site name instead of $arr[2]


    //or

    $str explode(".",explode("://",$arr[2]));
    // $str[1] is now the site name instead of $arr[2]

    //or there is many other ways to script it :P 

    Comment


      #3
      Originally posted by something else View Post
      PHP Code:
      $str preg_replace('/http:\/\/(.*)./is', $1$arr[2]);
      // $str is now the site name instead of $arr[2]


      //or

      $str explode(".",explode("://",$arr[2]));
      // $str[1] is now the site name instead of $arr[2]

      //or there is many other ways to script it :P 
      where i put this?

      PHP Code:
      function links_preg2($arr)
      {
          global 
      $set;
          if (
      preg_match('#^http://' preg_quote($_SERVER['HTTP_HOST']) . '#',$arr[2]))
          return 
      $arr[1] . '<a href="' $arr[2] . '">' $arr[2] . '</a>' $arr[4];
          else
          return 
      $arr[1] . '<a' . ($set['web'] ? ' target="_blank"' null) . ' href="http://' $_SERVER['HTTP_HOST'] . '/go.php?go=' base64_encode(html_entity_decode($arr[2])) . '">Link</a>' $arr[4];

      i can't do it, Warning: explode() expects parameter 2 to be string, array given in

      Comment


        #4
        Originally posted by tzapone View Post
        where i put this?

        i can't do it, Warning: explode() expects parameter 2 to be string, array given in
        oops i missed an array key out in the 2nd code >.<

        try this:
        PHP Code:
        function links_preg2($arr)
        {
            global 
        $set;
                
        $str preg_replace('/http:\/\/(.*)./is', $1$arr[2]); 
            if (
        preg_match('#^http://' preg_quote($_SERVER['HTTP_HOST']) . '#',$arr[2]))
            return 
        $arr[1] . '<a href="' $arr[2] . '">' $str '</a>' $arr[4];
            else
            return 
        $arr[1] . '<a' . ($set['web'] ? ' target="_blank"' null) . ' href="http://' $_SERVER['HTTP_HOST'] . '/go.php?go=' base64_encode(html_entity_decode($arr[2])) . '">Link</a>' $arr[4];

        Comment


          #5
          Originally posted by something else View Post
          oops i missed an array key out in the 2nd code >.<

          try this:
          PHP Code:
          function links_preg2($arr)
          {
              global 
          $set;
                  
          $str preg_replace('/http:\/\/(.*)./is', $1$arr[2]); 
              if (
          preg_match('#^http://' preg_quote($_SERVER['HTTP_HOST']) . '#',$arr[2]))
              return 
          $arr[1] . '<a href="' $arr[2] . '">' $str '</a>' $arr[4];
              else
              return 
          $arr[1] . '<a' . ($set['web'] ? ' target="_blank"' null) . ' href="http://' $_SERVER['HTTP_HOST'] . '/go.php?go=' base64_encode(html_entity_decode($arr[2])) . '">Link</a>' $arr[4];


          Parse error: syntax error, unexpected T_LNUMBER, expecting T_VARIABLE or '$' in on line
          PHP Code:
          $str preg_replace('/http:\/\/(.*)./is', $1$arr[2]); 
          Added after 3 minutes:

          fixed: i put $arr[1] instead $1 .

          Now i put
          PHP Code:
           '#^(https://|http://)?(www|m)?\.?(youtube\.com|youtu.be)(/watch(\.php)?\?v=|/v/|/)(?P<id>[a-zA-Z0-9-_]{11})(?P<rest>[\w&=/\?\.\#\+]+)?#ism''#' 
          in $str?

          Added after 17 minutes:

          i put this code:
          PHP Code:
          function links_preg2($arr

              global 
          $set
                  
          $str preg_replace('#(https://|http://)?(www|m)?\.?(youtube\.com|youtu.be)(/watch(\.php)?\?v=|/v/|/)(?P<id>[a-zA-Z0-9-_]{11})(?P<rest>[\w&=/\?\.\#\+]+)?#ism'$arr[1], $arr[2]);
                  
              if (
          preg_match('#^http://' preg_quote($_SERVER['HTTP_HOST']) . '#',$arr[2])) 
              return 
          $arr[1] . '<a href="' $arr[2] . '">' $str '</a>' $arr[4]; 
              else 
              return 
          $arr[1] . '<a' . ($set['web'] ? ' target="_blank"' null) . ' href="http://' $_SERVER['HTTP_HOST'] . '/go.php?go=' base64_encode(html_entity_decode($arr[2])) . '">Link</a>' $arr[4]; 

          function 
          links($msg)
          {
              global 
          $set;
              if (
          $set['bb_img'])$msg preg_replace_callback('/\[img\](.+)\[\/img\]/isU''img_preg'$msg);
              if (
          $set['bb_url'])$msg preg_replace_callback('/\[url=(.+)\](.+)\[\/url\]/isU''links_preg1'$msg);
              
              if (
          $set['bb_http'])$msg preg_replace_callback('~(^|\s)([a-z]+://([^ \r\n\t`\'"]+))(\s|$)~iu''links_preg2'$msg); 
          but don't work: when i put this link on status http://www.youtube.com/watch?v=YAJ9ArSlUG0 doesn't appear Link Youtube, only Link
          Last edited by tzapone; 08.12.13, 06:57.

          Comment


            #6
            ok i tried it myself and i forgot the quotes around the "$1"

            So i tested this:
            PHP Code:
            <?php
            error_reporting
            (E_ALL);
            ini_set('display_errors'1);
            $arr[2] = "http://google.com";
            $str preg_replace('/http:\/\/(.*)./is'"$1"$arr[2]); 
            echo 
            $str;
            ?>
            However this gave an unexpected result: google.co to which i am slighhtly confused to why it is doing that >.< arnage is good at preg_replace so he might be able to tell whats wrong with it.


            anyway here is a working version:
            PHP Code:
            function links_preg2($arr

                global 
            $set
                 
            $str explode("://",$arr[2]);
                
            $str explode(".",$str[1]); 
                if (
            preg_match('#^http://' preg_quote($_SERVER['HTTP_HOST']) . '#',$arr[2])) 
                return 
            $arr[1] . '<a href="' $arr[2] . '">' $str[0] . '</a>' $arr[4]; 
                else 
                return 
            $arr[1] . '<a' . ($set['web'] ? ' target="_blank"' null) . ' href="http://' $_SERVER['HTTP_HOST'] . '/go.php?go=' base64_encode(html_entity_decode($arr[2])) . '">Link</a>' $arr[4]; 

            Comment


              #7
              PHP Code:
              function links_preg2($arr)  
              {  
                  global 
              $set;  
                   
              $str explode("://",$arr[2]); 
                  
              $str explode("/",$str[1]);  
                  if (
              preg_match('#^http://' preg_quote($_SERVER['HTTP_HOST']) . '#',$arr[2]))  
                  return 
              $arr[1] . '<a href="' $arr[2] . '">' $str[0] . '</a>' $arr[4];  
                  else  
                  return 
              $arr[1] . '<a' . ($set['web'] ? ' target="_blank"' null) . ' href="http://' $_SERVER['HTTP_HOST'] . '/go.php?go=' base64_encode(html_entity_decode($arr[2])) . '">Link '.$str[0].'</a>' $arr[4];  

              I did it with this function :D

              Thank's for the help.

              The topic can be closed.

              Comment


                #8
                Your welcome

                btw error was with (.*) regex in the preg_replace it needed [^a-zA-Z0-9] regex
                the end "." must be to go back 1 letter which produce the .co instead of .com

                Anyway, using explode or preg_replace fails due to sub-domains eg: http://m.youtube.com which will produces the letter "m"

                but that's as far as I'm willing to script atm :P

                Comment

                Working...
                X