• Analyzing URLs as Links to the resource using a PHP function

    This is PHP function split_url_fuction() writter for twitter like application useful to split URL from the updated sentence(posted message), then URL changing like tinyurl and link to the resource.

    This function is break up the URL from the sentence, if the URL string length greater than 30 words it’s change like TinyURL.

    Step 1. You have to include the file split_url_function.php into the PHP file that will use the function.

    1
    
    include("split_url_function.php");

    Now you have to pass the updated data:

    1
    
    <?php echo  split_url_function($message); ?>

    This is the code of split_url_funtion() PHP function:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    
    <?php
    include("tiny_url.php"); //tinyurl function
    function split_url_function($update)   
    {
        //----URL Checking in Update.
        $http= substr_count($update,"http://");
        $www=substr_count($update,"www.");
        $htp=substr_count($update,"http//");
     
        if($http==1)
       {
             $str="http://";
             $h=1;
        }
        if($www==1)
       {
          $str="www.";
          $w=1;
        }
        if($h==1 && $w==1)
        {
           $str='http://www.'; //--Both
        }
        if($htp==1)
        {
              $comb_str=$update; //--No url
         }
    //--- explode the update
    $fa=explode($str,$update);
    $cnt_fa=count($fa);
    $se=explode(" ",$fa[1]);
    $cnt_se=count($se);
     
    for($i=1;$i<=$cnt_se;$i++)
    {
         $sep.=$se[$i].' ';
    }
         // Split URL
          $split_url=$str.$se[0];
          if($split_url=='')
         {
             $final_update=$update;
          }
          else
        {
             // URL link sting lenght verfication
             if(strlen($split_url)>=30)
            {
                 $tiny = tiny_url($split_url);
    $final_update=$fa[0].'<a href="'.$tiny.'" target="_blank">'.$tiny.'</a>'.$sep;
    echo $final_update;
             }
             else
            {
              // Combine all the explode parts
    $final_update=$fa[0].'<a href="'.$split_url.'" target="_blank">'.$split_url.'</a>'.$sep;
    echo $final_update;
             }
         }
    }
    ?>

    tiny_url.php

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    
    <?php
    function tiny_url($url)
    {
    $ch = curl_init();
    $timeout = 5;
    curl_setopt($ch,CURLOPT_URL,'http://tinyurl.com/api-create.php?url='.$url);
    curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
    curl_setopt($ch,CURLOPT_CONNECTTIMEOUT,$timeout);
    $data = curl_exec($ch);
    curl_close($ch);
    return $data;
    }
    ?>

Comments on this post

Leave a Reply

  • Security Code :


    − 2 = six