Skip to content

Instantly share code, notes, and snippets.

@StephanStanisic
Last active March 27, 2016 11:02
Show Gist options
  • Save StephanStanisic/62a1f44f388244834dd9 to your computer and use it in GitHub Desktop.
Save StephanStanisic/62a1f44f388244834dd9 to your computer and use it in GitHub Desktop.
Tinyurl php function, a simple 5-line url shortner! PHP url shortner - JavaScript url shortner

#The most simple PHP url shortner! This code uses tinyurl.com to short urls, so thanks to the cretor of the awesome site.
Looking for a JS version? Go here: https://gist.github.com/StephanStanisic/b7017b1a8cd3c785e332. I also have a PHP version there (one line!).

##Usage:
tinyurl("http://google.com") returns http://tinyurl.com/2tx
tinyurl("http://github.com") returns http://tinyurl.com/2lekkm

##Sample application

<?php
function tinyurl($url) {
  $data = file_get_contents('http://tinyurl.com/create.php?url='.urlencode($url));
  preg_match_all ("/<b>([^`]*?)<\/b>/", $data, $matches);
  return $matches[1][1];
}
if( isset( $_GET["url"] ) ) {
  echo "Your short url: ".tinyurl($_GET["url"]);
}
?>

<form style="text-align: center;">
  <h1>Url shortner</h1>
  <input type="text" name="url" placeholer="Url to short here" /><br />
  <input type="submit" value="Short!" />
</form>
function tinyurl($url) {
$data = file_get_contents('http://tinyurl.com/create.php?url='.urlencode($url));
preg_match_all ("/<b>([^`]*?)<\/b>/", $data, $matches);
return $matches[1][1];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment