Diese PHP Funktion verwandelt einen als Text eingefügten Link automatisch zu einem anklickbaren Link.
function dolink($subject) {
$muster = "/(http:\/\/)?([a-zA-Z0-9\-.]+\.[a-zA-Z0-9\-]+([\/]([a-zA-Z0-9_\/\-.?&%=+])*)*)/";
$ersetzen = "<a href="\"http://$2\"">$2</a>";
return preg_replace($muster, $ersetzen, $subject);
}
Mit der folgende Routine können URL´s mit Googles Kurz-URL Dienst goo.gl gekürzt werden. Das Script erfordert einen kostenlosen Google API Key.
function getgoogl($glurl, $apikey) {
$userurl = rawurlencode($glurl);
$ch = curl_init('https://www.googleapis.com/urlshortener/v1/url?key='.$apikey);
curl_setopt_array($ch, array(
CURLOPT_POST => TRUE,
CURLOPT_RETURNTRANSFER => TRUE,
CURLOPT_HTTPHEADER => array('Content-type: application/json'),
CURLOPT_POSTFIELDS => json_encode(array('longUrl' => $userurl))
));
$chresults = json_decode(curl_exec($ch));
curl_close($ch);
return $chresults->id;
}<?php
function isValidURL($value) {
$value = trim($value);
$validhost = true;
if (strpos($value, 'http://') === false && strpos($value, 'https://') === false) {
$value = 'http://'.$value;
}
//first check with php's FILTER_VALIDATE_URL
if (filter_var($value, FILTER_VALIDATE_URL, FILTER_FLAG_HOST_REQUIRED) === false) {
$validhost = false;
} else {
//not all invalid URLs are caught by FILTER_VALIDATE_URL
//use our own mechanism
$host = parse_url($value, PHP_URL_HOST);
$dotcount = substr_count($host, '.');
//the host should contain at least one dot
if ($dotcount > 0) {
//if the host contains one dot
if ($dotcount == 1) {
//and it start with www.
if (strpos($host, 'www.') === 0) {
//there is no top level domain, so it is invalid
$validhost = false;
}
} else {
//the host contains multiple dots
if (strpos($host, '..') !== false) {
//dots can't be next to each other, so it is invalid
$validhost = false;
}
}
} else {
//no dots, so it is invalid
$validhost = false;
}
}
//return false if host is invalid
//otherwise return true
return $validhost;
}
?>Code by Koen Ekelschot