Shorten url text in links like stackoverflow?

I need help in PHP to create short URLs, just as StackOverflow creates when we comment on any long URL in the comments to the question.

StackOverflow shortens the http://www.noupe.com/how-tos/10-ways-to-automatically-manually-backup-mysql-database.html long URL for short URLs like this noupe.com/...

I need similar functionality in my application. Can anyone give any idea or code on how to do this in PHP?

I was tired of looking for it on StackOverflow, but did not find any questions. I remember that I saw this type of question on SO, but right now I can’t find it :(

Please, help!

+4
source share
5 answers

Just a simplified algorithm diagram.

  • See if the link is longer than X characters.
  • Remove http:// or https:// at the beginning with str_replace .
  • Expand to / and save only the first element in the returned array.
  • If you found more than one item in step 3, add /... to the end.
  • Additionally. Remove www. at the beginning using str_replace .

Using the found string, calling it [shortURL] , you create your anchor:

 <a href="[fullURL]">[shortURL]</a> 
+7
source

I assume that you just need to look for the <a> tags in the original release and modify it accordingly. href remains unchanged, but you change the name of the link to whatever you want.

But this is only one idea ... You can always experiment with new things.

There should also be a way to accomplish this with javascript on-the-go.

Think about it!

+2
source

Here is the function for replacing urls with links. You just need to customize how you want to format it. Maybe use parse_url()

 <?php function URLref($sentence){ $temp = explode(" ", $sentence); $new = ""; foreach($temp as $i){ if(preg_match('([A-Za-z][A-Za-z0-9+.-]{1,120}:[A-Za-z0-9/](([A-Za-z0-9$_.+!*,;/?:@&~=-])|%[A-Fa-f0-9]{2}){1,333}(#([a-zA-Z0-9][a-zA-Z0-9$_.+!*,;/?:@&~=%-]{0,1000}))?)', $i)){ $new .= '<a href="'.$i.'">'.$i.'</a>'; }else{ $new .= "$i "; } } return trim($new); } $sentence = "My site ULR is http://www.google.com/lolz.html"; echo URLref($sentence); 
+1
source

You can get URLs using regular expressions, here are two examples of creating <a> tags from found URLs and how to reduce the content of <a> tags:

 <?php $orig_text = <<<TEXT This is some text. http://www.example.com/this-is-a-quite-long-url-to-be-shortened.html http://www.example.com/another-url-to-be-shortened and http://www.example.com/another-one-that-is-longer-than-limit then http://www.example.com/an-ok-url and some text to finish the sentence. Now, try with an HTTPS url: https://www.example.com/this-https-url-is-too-long. And with an already-created tag <a href='http://www2.example.com/this-is-another-long-url.html'>http://www2.example.com/this-is-another-long-url.html</a> <a href='http://www2.example.com/my-test-url-goes-here.html'>And this is just some long long link description to be shortened</a>. More text here. TEXT; $PATTERN_URL='#(?:href=[\'"]?)?!(https?://([^/]+)/([^\s]+))\b#'; define('URL_LENGTH_LIMIT', 36); function create_a_tag($matches) { $url = $matches[1]; $label = $matches[1]; if (strlen($label) > URL_LENGTH_LIMIT) $label = $matches[2] . '/...'; return "<a href='$url'>$label</a>"; } function shorten_url_or_text($url) { if (strlen($url) > URL_LENGTH_LIMIT) { $matches = array(); if (preg_match('#^(https?://[^/]*).*#', $url, $matches)) { // Shorten as for URLS return $matches[1] . '/...'; } else { // Trim to a given length return substr($url, 0, URL_LENGTH_LIMIT-3) . '...'; } } else { return $url; } } function shorten_a_text($matches) { $text = shorten_url_or_text($matches[2]); return $matches[1] . $text . $matches[3]; } // This will replace urls with their shortened form echo "----- CREATE <A> TAGS -----\n"; $text2 = preg_replace_callback($PATTERN_URL, 'create_a_tag', $orig_text); echo $text2 . "\n"; // This will shorten content inside <a> tags echo "----- CREATE <A> TAGS -----\n"; $text3 = preg_replace_callback('@(<a[^>]*>)([^<]*)(</a>)@i', 'shorten_a_text', $text2); echo $text3; echo "\n"; 
+1
source

To create a β€œcustom” URL that does not have a corresponding file, you need to configure your web server. If you use Apache and have the rights to it, you can take a look at mod_rewrite : http://httpd.apache.org/docs/1.3/mod/mod_rewrite.html

And the tutorial: http://articles.sitepoint.com/article/guide-url-rewriting

-3
source

All Articles