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!
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 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!
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:
I would suggest actually using an ellipsis, instead of three periods (which is not the same thing): $url = substr($url, 0, 30) . '…'; Code (markup): And please, stop using the double quotes for strings in PHP...
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.
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.
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.".
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.
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
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.
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.
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.
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 "…".
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.