SitePoint Sponsor

User Tag List

Results 1 to 6 of 6

Thread: substr trouble

  1. #1
    SitePoint Member
    Join Date
    Jun 2006
    Posts
    12
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    substr trouble

    Hi;
    When i enter these codes
    PHP Code:
    $lasttw "<a href=\"$row[fullurl]\">$row[fullurl]</a><br />";
    $lasttwshort = (strlen($lasttw)>53)?substr($lasttw,0,53).'...<br />':$lasttw;
    echo 
    "$lasttwshort"
    or

    PHP Code:
    $lasttw "<a href=\"$row[fullurl]\">$row[fullurl]</a><br />";
    if (
    strlen($lasttw) > 53) {
      
    $lasttw substr($lasttw053) . "...<br />";
    }
    echo 
    "$lasttw"
    i'm getting page like this;

    Code:
    http://www.yahoo.comhttp://www.youtu...
    http://www.foru...
    http://www.sitepoin...
    http://http...
    http://www.boardz...
    http://www.gamezo...
    http://rapidshare.dehttp://killervide.com...
    http://www.forum...
    http://www.forum...
    ...
    ...
    http://
    ...
    but i wanna get page like this;

    Code:
    http://www.yahoo.com/
    http://www.sitepoint.com/forums/newthread.php?do=newt...
    etc
    (if the link is longer than 53 chars substr should add "..." automatically)

    Thanks for your help...Have a nica day...

  2. #2
    SitePoint Guru Ize's Avatar
    Join Date
    Nov 2005
    Location
    The Netherlands
    Posts
    808
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I use this function:

    PHP Code:
        /* make hyperlinks look pretty by shortening them a bit */
        
    function shortenURL ($matches)
        {
            
    $match        $matches[0];
            
    $a_start    "<a href=\"$match\" rel=\"rel_nofollow\">";
            
    $a_end        "</a>";
            if (
    strlen($match)>40)
            {
                
    $str_start    substr($match,0,15);
                
    $str_end    substr($match,(strlen($match)-15),15);
                
    $match        $str_start '...' $str_end;
            }
            return 
    $a_start.$match.$a_end;
        } 
    It is currently used with preg_replace_callback, but it's easy to modify it to work with just a string as the parameter. Hope that helps.

  3. #3
    SitePoint Member
    Join Date
    Jun 2006
    Posts
    12
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks for your help Ize...
    i look preg_replace_callback from php.net/preg_replace_callback but i didn't understand anything about that

  4. #4
    SitePoint Guru Ize's Avatar
    Join Date
    Nov 2005
    Location
    The Netherlands
    Posts
    808
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I've altered the code for you:

    PHP Code:
    /* make hyperlinks look pretty by shortening them a bit */
        
    function shortenURL ($match)
        {
            
    $a_start    "<a href=\"$match\" rel=\"rel_nofollow\">";
            
    $a_end        "</a>";
            if (
    strlen($match)>40)
            {
                
    $str_start    substr($match,0,15);
                
    $str_end    substr($match,(strlen($match)-15),15);
                
    $match        $str_start '...' $str_end;
            }
            return 
    $a_start.$match.$a_end;
        } 
    You can now call the function like this:

    PHP Code:
    print shortenURL ("http://www.sitepoint.com/forums"); 
    Edit: you might want to play around with the numbers to suit your needs. As-is, the function will manipulate only strings that contain more than 40 characters.

  5. #5
    SitePoint Member
    Join Date
    Jun 2006
    Posts
    12
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    thanks but it gives this output;
    Code:
    http://" rel="rel_nofollow">http://

  6. #6
    SitePoint Member
    Join Date
    Jun 2006
    Posts
    12
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I got what i want with this code;

    Code:
    $fullurl = $row["fullurl"]; 
    if (strlen($fullurl) > 53) { 
        $showurl = substr($fullurl, 0, 53) . "..."; 
    }else{ 
        $showurl = $fullurl; 
    } 
    
    $lasttw = "<a href=\"$fullurl\">$showurl</a><br />"; 
    
    echo "$lasttw";
    Thanks for your help Ize.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •