SitePoint Sponsor

User Tag List

Results 1 to 8 of 8
  1. #1
    Almeaty Member
    Join Date
    Jul 2000
    Location
    Nowhere
    Posts
    278
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    How to shorten a string?

    Hi, I'm building a script and I noticed that long URLs are pulling the whole layout apart. So I wanted to include a function that shortens such URLs (or generally long strings) downto a given number of chars. So something like
    Code:
    if ( strlen($string) > 50 )
    {
       // shorten it
    }
    Do you know a solution?

    Thanks!

  2. #2
    Making a better wheel silver trophy DR_LaRRY_PEpPeR's Avatar
    Join Date
    Jul 2001
    Location
    Missouri
    Posts
    3,428
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    you mean like this board does to long URLs? i would like to know that too.

    of course you have to find the long string first and then if it's more than a certain length, say 50 chars in your ex., take like 23 chars from the front, and put 3 periods (...), and then 23 chars from the end.

    something like that...
    - Matt ** Ignore old signature for now... **
    Dr.BB - Highly optimized to be 2-3x faster than the "Big 3."
    "Do not enclose numeric values in quotes -- that is very non-standard and will only work on MySQL." - MattR

  3. #3
    Almeaty Member
    Join Date
    Jul 2000
    Location
    Nowhere
    Posts
    278
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    yep, that's what I thought of... or maybe somekind of function that already stores the shortened string in a variable. and we just need to put the ... behind

  4. #4
    ********* Callithumpian silver trophy freakysid's Avatar
    Join Date
    Jun 2000
    Location
    Sydney, Australia
    Posts
    3,798
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You want:
    string substr (string string, int start [, int length])

    http://www.php.net/manual/en/function.substr.php
    PHP Code:
    // shorten string and add '...'
    $shortString substr($longString047) . '...'
    Last edited by freakysid; Aug 12, 2001 at 16:31.

  5. #5
    SitePoint Wizard
    Join Date
    Jul 1999
    Location
    Chicago
    Posts
    2,629
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Well, it really depends what type of string it is. Is it one of those huge ropes, or is it just fishing line? Usually you can just cut fishing line to the length you'd like with scissors but you will need a saw to cut through thick strings like climbing ropes.

    Also don't forget to measure twice, as sometimes you will waste a while piece of string by cutting it too short.

    Let us know how it goes.

  6. #6
    Making a better wheel silver trophy DR_LaRRY_PEpPeR's Avatar
    Join Date
    Jul 2001
    Location
    Missouri
    Posts
    3,428
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Originally posted by qslack
    Well, it really depends what type of string it is. Is it one of those huge ropes, or is it just fishing line? Usually you can just cut fishing line to the length you'd like with scissors but you will need a saw to cut through thick strings like climbing ropes.

    Also don't forget to measure twice, as sometimes you will waste a while piece of string by cutting it too short.

    Let us know how it goes.
    LOL

  7. #7
    Almeaty Member
    Join Date
    Jul 2000
    Location
    Nowhere
    Posts
    278
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    cool thanks!

    LOL, now I have a problem I almost had expected: How do I actually find that URL?? I mean, there's this user's posted message where the URL needs to be filtered out... currently (without the shortening process) I have solved it this way to replace the found internet address:

    PHP Code:
    $message eregi_replace"[^[url]http://[/url]](www\.(([A-Za-z0-9~&=;\?%_#./\-])*)([a-zA-Z0-9/]))"," [url=http://\\1]\\1[/url]",$message );
    $message eregi_replace"^([url]http://[/url](([A-Za-z0-9~&=;\?%_.#/\-])*)([a-zA-Z0-9/]))"," [url=\\1]\\1[/url]",$message ); 
    Maybe I have to change it entirely? hmm...

  8. #8
    Almeaty Member
    Join Date
    Jul 2000
    Location
    Nowhere
    Posts
    278
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    hmm.. anyone? sorry for the re-post. it's just to keep this thread on top


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
  •