25 lines
1.2 KiB
PHP
25 lines
1.2 KiB
PHP
<?php
|
|
|
|
/*
|
|
$tldsregex has to be a string with a list (better if reverse ordered) of valid
|
|
tlds separated by "|"; you can get such a list by adding "require gettlds.php"
|
|
in the calling script and using it like this:
|
|
$tldsre=gettlds($path_to_tlds_cache_file,true);
|
|
Then you can pass the list to postLength like this: $length=postLength($post,$tldsre['tlds']);
|
|
($tldsre['warnings'] will contain possible gettlds warnings)
|
|
*/
|
|
|
|
function postLength($post,&$tldsregex) {
|
|
// echo "-A-> |{$post}|\n";
|
|
// for some reason, mastodon seems to check tld existence only on http(s) links - see next regexp
|
|
$res=preg_replace('#(^|\W)(@[a-zA-Z0-9_]+)@(([a-z0-9]([a-z0-9-]+[a-z0-9])?){1,63}\.)+([a-z0-9]([a-z0-9-]+[a-z0-9])?){1,63}\b#u', '$1$2', $post);
|
|
if (!is_null($res)) $post=$res;
|
|
// $res=preg_replace('#(^|\W)https?://(([a-z0-9]([a-z0-9-]+[a-z0-9])?){1,63}\.)+([a-z0-9]([a-z0-9-]+[a-z0-9])?){1,63}(/\S*[\w=?_-])?#u', '$1HTTP://UUUUUUUUUUUUUUUU', $post);
|
|
// on http(s) links mastodon checks if tld exists...
|
|
$res=preg_replace('#(^|\W)https?://(([a-z0-9]([a-z0-9-]+[a-z0-9])?){1,63}\.)+('.$tldsregex.')(/\S*[\w/=_\-])?#u', '$1UUUUUUUUUUUUUUUUUUUUUUU', $post);
|
|
if (!is_null($res)) $post=$res;
|
|
// echo "-B-> |{$post}|\n";
|
|
return mb_strlen($post,'UTF-8');
|
|
}
|
|
|
|
?>
|