1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

Shortening a URL with preg_replace

Discussion in 'PHP' started by qwikad.com, Mar 31, 2015.

  1. #1
    What would be the easiest way to shorten a URL with preg_replace? Let's say a URL looks like this:

    http:/ /someurl.com/file_1/something_else/new/file_2.html

    I want to reduce it to let's say 30 characters:

    http:/ /someurl.com/file_1/some...

    By the way, the link doesn't have to work. I just want it for a preview and it doesn't matter if it works or not. Just shortened.



    Thanks!
     
    Last edited: Mar 31, 2015
    qwikad.com, Mar 31, 2015 IP
  2. NetStar

    NetStar Notable Member

    Messages:
    2,471
    Likes Received:
    541
    Best Answers:
    21
    Trophy Points:
    245
    #2
    preg_replace is not the right tool for the job. Use substr to truncate a string like so:

    
    <?php
    
    $url = "http://someurl.com/file_1/something_else/new/file_2.html";
    $url = substr($url, 0, 30);
    print $url . "...";
    
    ?>
    
    PHP:
     
    NetStar, Mar 31, 2015 IP
  3. qwikad.com

    qwikad.com Illustrious Member Affiliate Manager

    Messages:
    7,151
    Likes Received:
    1,656
    Best Answers:
    29
    Trophy Points:
    475
    #3
    @NetStar

    I should've clarified. What if it's not that specific URL. I am trying to do this for any URL. In other words, if a URL is longer than 30 characters - shorten it.

    Thanks!
     
    Last edited: Mar 31, 2015
    qwikad.com, Mar 31, 2015 IP
  4. NetStar

    NetStar Notable Member

    Messages:
    2,471
    Likes Received:
    541
    Best Answers:
    21
    Trophy Points:
    245
    #4
    Doesn't matter. Just assign the url to the variable $url or whatever you want to call it. The substr() is the function that can truncate.
    substr(THE_URL_HERE, STARTING_POINT, HOW_MANY_CHAR_TO_SAVE);

    
    $url = "some url here";
    
    if (length($url) > 30)
    {
       $url = substr($url, 0, 30) . "...";
    }
    
    PHP:
     
    NetStar, Mar 31, 2015 IP
  5. PoPSiCLe

    PoPSiCLe Illustrious Member

    Messages:
    4,623
    Likes Received:
    725
    Best Answers:
    152
    Trophy Points:
    470
    #5
    I would suggest actually using an ellipsis, instead of three periods (which is not the same thing):
    
    $url = substr($url, 0, 30) . '&#8230;';
    
    Code (markup):
    And please, stop using the double quotes for strings in PHP...
     
    PoPSiCLe, Apr 1, 2015 IP
  6. NetStar

    NetStar Notable Member

    Messages:
    2,471
    Likes Received:
    541
    Best Answers:
    21
    Trophy Points:
    245
    #6
    It really doesn't matter in the above example. And if the performance difference of 1% of a tenth of a fraction of a micro micro micro second matter to you, abandon PHP all together!! But yes the "best" (but still irrelevant) practice would be to use single quotes.

    And using an ellipsis vs 3 periods in a row doesn't matter. There's really no benefit over individual preference. Personally I would use the 3 periods to avoid my code from appearing cryptic.
     
    NetStar, Apr 4, 2015 IP
    ThePHPMaster likes this.
  7. PoPSiCLe

    PoPSiCLe Illustrious Member

    Messages:
    4,623
    Likes Received:
    725
    Best Answers:
    152
    Trophy Points:
    470
    #7
    When it comes to the single quotes vs. double quotes, I usually prefer to teach the better way to do it, simply because it makes the code look a lot nicer, especially if you're putting HTML in the strings. The speed difference is negligible, unless you have ENORMOUS amounts of string parsing going on, but yes.

    Uhm... there's DEFINITELY typographical difference - three periods is NOT an ellipsis - an ellipsis is an actual element in typography, and is NOT the same thing as 3 periods after one another. So saying that there's no difference is plain wrong. I usually doesn't bother when typing out things in comments and such, but when putting text on a website, or in a document, not using the proper ellipsis just looks sloppy (to those who know the difference).
    An easy what to do it is to simply put a replacement function into whatever parser of content you have already.
     
    PoPSiCLe, Apr 4, 2015 IP
  8. NetStar

    NetStar Notable Member

    Messages:
    2,471
    Likes Received:
    541
    Best Answers:
    21
    Trophy Points:
    245
    #8
    No it's not the same exact character output, but it's the same exact visual output to the end user. There is no benefit in using it over 3 periods. There's no reason why the OP should use the horizontal ellipse html char code over typing out the periods. There's nothing to gain. The visitor doesn't see a difference. The web site doesn't see a difference in performance. The developer doesn't even have a benefit. Hell, he's actually typing more and now has to remember what that damn char code means. It's actually completly irrelevant for you to even suggest the use of it as if it mattered. It has NOTHING to do with a best practice.

    If I was to ask how do you disengage a clutch and the response was "with your foot" it would be pretty damn pointless for someone to follow that with "for the love of god!!! it's your shoe not your foot! Press it with your shoe! And stop wearing Reboks you need to start wearing Nike's.".
     
    Last edited: Apr 5, 2015
    NetStar, Apr 5, 2015 IP
  9. PoPSiCLe

    PoPSiCLe Illustrious Member

    Messages:
    4,623
    Likes Received:
    725
    Best Answers:
    152
    Trophy Points:
    470
    #9
    Uhm - an ellipsis and three periods represent different VISUAL styles - that's the whole point. The kerning on an ellipsis is not as wide as three periods, and while it seems like a miniscule issue, it's still WRONG. If you don't understand the concept, that's fine, that just means you're not a graphic designer, but claiming that they're "exactly the same" just goes to show how little you know.
     
    PoPSiCLe, Apr 5, 2015 IP
  10. NetStar

    NetStar Notable Member

    Messages:
    2,471
    Likes Received:
    541
    Best Answers:
    21
    Trophy Points:
    245
    #10
    It's 3 dots. It's so irrelevant to push LOL.. It's just an individual preference. Not a best practice. Not a standard. Not the right or wrong way. You know that Lol
     
    NetStar, Apr 5, 2015 IP
  11. PoPSiCLe

    PoPSiCLe Illustrious Member

    Messages:
    4,623
    Likes Received:
    725
    Best Answers:
    152
    Trophy Points:
    470
    #11
    It IS a standard, actually, in print. But you don't care, nor do most of the dyslexic morons writing news, briefs, comments, articles or other published media. No worries, I for one has given up a long time ago.
     
    PoPSiCLe, Apr 5, 2015 IP
  12. NetStar

    NetStar Notable Member

    Messages:
    2,471
    Likes Received:
    541
    Best Answers:
    21
    Trophy Points:
    245
    #12
    Well I'm glad you has given up on pushing standards in languages... (3 Periods)

    Now the original poster knows how to type more characters to show 3 dots and that he should use single over double quotes for 0 noticable performance gains. *rolls eyes*. I know you just wanted to correct someone but in return you looked like an idiot trying to do so. Thank you so much for the value of your contribution.
     
    NetStar, Apr 6, 2015 IP
  13. PoPSiCLe

    PoPSiCLe Illustrious Member

    Messages:
    4,623
    Likes Received:
    725
    Best Answers:
    152
    Trophy Points:
    470
    #13
    That would be have given up, but hey, grammar is hard *rolls eyes*.
    Besides, using single quotes around output can actually also make the code cleaner, for those of use who care, as you won't need to escape double quotes. Of course that can also be done using HEREDOC, but that isn't always feasible, nor necessarily a good idea.
     
    PoPSiCLe, Apr 6, 2015 IP
  14. NetStar

    NetStar Notable Member

    Messages:
    2,471
    Likes Received:
    541
    Best Answers:
    21
    Trophy Points:
    245
    #14
    That would be an aesthetic preference. Personally (in my opinion... like yours was an opinion too) I see no difference in ' and " when it comes to writing clean code. And if you believe it is due to less "character polution" than "..." should be cleaner than "&#8230;".
     
    NetStar, Apr 6, 2015 IP
  15. PoPSiCLe

    PoPSiCLe Illustrious Member

    Messages:
    4,623
    Likes Received:
    725
    Best Answers:
    152
    Trophy Points:
    470
    #15
    It IS cleaner, and faster, to write:
    
    $var = '<p class="one two three"><span class="something">blah</span> this goes here</p>';
    
    Code (markup):
    Than writing:
    
    $var="<p class=\"one two three\"><span class=\"something\">blah</span> this goes here</p>";
    
    Code (markup):
    Now, this is a very simple example, but if you output whole webpages with PHP, the amount of backslashes build up, and it's easy to forget one, which then breaks the code :/
    Of course it's a matter of opinion, but it do mess up the code, and if you're really unlucky, you'll have some issues with parsing the code returned via JSON, for instance. Using single quotes is just simpler.
    Not to mention that backslashes often kills syntax highlighting, as evidenced on this very page.
     
    PoPSiCLe, Apr 6, 2015 IP
  16. NetStar

    NetStar Notable Member

    Messages:
    2,471
    Likes Received:
    541
    Best Answers:
    21
    Trophy Points:
    245
    #16
    Irrelevant example. We are talking about "..." vs '...'. No difference.
     
    NetStar, Apr 8, 2015 IP