226 lines
5.1 KiB
PHP
226 lines
5.1 KiB
PHP
<?php
|
|
declare(strict_types=1);
|
|
|
|
function umlaute(string $str): string {
|
|
return str_replace ( array (
|
|
'Ä',
|
|
'Ö',
|
|
'Ü',
|
|
'ä',
|
|
'ö',
|
|
'ü',
|
|
'ß',
|
|
'&'
|
|
), array (
|
|
'Ä',
|
|
'Ö',
|
|
'Ü',
|
|
'ä',
|
|
'ö',
|
|
'ü',
|
|
'ß',
|
|
'&'
|
|
), $str );
|
|
}
|
|
function chk(string $str): string {
|
|
return str_replace ( "'", '"', $str );
|
|
}
|
|
function noScript(string $str): string {
|
|
return str_replace ( array (
|
|
'<',
|
|
'>'
|
|
), array (
|
|
'<',
|
|
'>'
|
|
), $str );
|
|
}
|
|
function random(int $name_laenge): string {
|
|
$zeichen = "abcedfghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRTSUVWXYZ0123456789";
|
|
$name_neu = "";
|
|
|
|
@mt_srand ( ( double ) microtime () * 1000000 );
|
|
for($i = 0; $i < $name_laenge; $i ++) {
|
|
$r = mt_rand ( 0, strlen ( $zeichen ) - 1 );
|
|
$name_neu .= $zeichen[$r];
|
|
}
|
|
return $name_neu;
|
|
}
|
|
function startsWith(string $haystack, string $needle): bool {
|
|
$length = strlen ( $needle );
|
|
return (substr ( $haystack, 0, $length ) === $needle);
|
|
}
|
|
function endsWith(string $haystack, string $needle): bool {
|
|
$length = strlen ( $needle );
|
|
|
|
return $length === 0 || (substr ( $haystack, - $length ) === $needle);
|
|
}
|
|
function onlyAlpha(string $str, string $zus = ''): string {
|
|
return preg_replace ( "/[^a-zA-Z0-9 \-\{$zus}_]+/", "", $str );
|
|
}
|
|
function shortener(string $str, int $len = 50, string $fill = '...'): string {
|
|
if (strlen ( $str ) > $len) {
|
|
$str = substr ( $str, 0, $len - strlen ( $fill ) ) . $fill;
|
|
}
|
|
return $str;
|
|
}
|
|
function isEmail(string $str): string|false {
|
|
$match = preg_match ( "/[a-zA-Z0-9\-\_\.]*\@[a-zA-Z0-9\-\_\.]*.[a-z]{2,10}/", $str );
|
|
if ($match) {
|
|
return $str;
|
|
}
|
|
return false;
|
|
}
|
|
function markUp(string $text): string {
|
|
$r = '';
|
|
$lv = 0;
|
|
foreach ( explode ( "\n", $text ) as $t ) {
|
|
$nlv = 0;
|
|
if (startsWith ( '**** ', $t )) {
|
|
$t = substr ( $t, 5 );
|
|
$nlv = 4;
|
|
}
|
|
if (startsWith ( '*** ', $t )) {
|
|
$t = substr ( $t, 4 );
|
|
$nlv = 3;
|
|
}
|
|
if (startsWith ( '** ', $t )) {
|
|
$t = substr ( $t, 3 );
|
|
$nlv = 2;
|
|
}
|
|
if (startsWith ( '* ', $t )) {
|
|
$t = substr ( $t, 2 );
|
|
$nlv = 1;
|
|
}
|
|
if ($lv != $nlv) {
|
|
while ( $lv < $nlv ) {
|
|
$text .= '<ul>';
|
|
$lv ++;
|
|
}
|
|
while ( $lv > $nlv ) {
|
|
$text .= '</ul>';
|
|
$lv --;
|
|
}
|
|
}
|
|
if (startsWith ( $t, '!!!!! ' )) {
|
|
$t = '<h5>' . substr ( $t, 6 ) . '</h5>';
|
|
}
|
|
if (startsWith ( $t, '!!!! ' )) {
|
|
$t = '<h4>' . substr ( $t, 5 ) . '</h4>';
|
|
}
|
|
if (startsWith ( $t, '!!! ' )) {
|
|
$t = '<h3>' . substr ( $t, 4 ) . '</h3>';
|
|
}
|
|
if (startsWith ( $t, '!! ' )) {
|
|
$t = '<h2>' . substr ( $t, 3 ) . '</h2>';
|
|
}
|
|
if (startsWith ( $t, '! ' )) {
|
|
$t = '<h1>' . substr ( $t, 2 ) . '</h1>';
|
|
}
|
|
if ($lv == 0) {
|
|
$r .= $t;
|
|
} else {
|
|
$r .= '<li>' . $t . '</li>';
|
|
}
|
|
// var_dump ( $t );
|
|
}
|
|
while ( $lv > 0 ) {
|
|
$r .= '</ul>';
|
|
$lv --;
|
|
}
|
|
return $r;
|
|
}
|
|
function onlySimpleHTML(string $s, ?array $allowedTags = null): string {
|
|
if ($s === '') {
|
|
return '';
|
|
}
|
|
|
|
if ($allowedTags === null) {
|
|
$allowedTags = array (
|
|
'b',
|
|
'u',
|
|
'i',
|
|
'span',
|
|
'br',
|
|
'h1',
|
|
'h2',
|
|
'h3',
|
|
'h4',
|
|
'h5',
|
|
'h6',
|
|
'li',
|
|
'ul',
|
|
'ol',
|
|
'pre'
|
|
);
|
|
}
|
|
|
|
$allow = array_fill_keys ( array_map ( 'strtolower', $allowedTags ), true );
|
|
$selfClosing = array (
|
|
'br' => true
|
|
);
|
|
|
|
$parts = preg_split ( '/(<[^>]*>)/', $s, - 1, PREG_SPLIT_DELIM_CAPTURE );
|
|
if ($parts === false) {
|
|
return htmlspecialchars ( $s, ENT_QUOTES | ENT_SUBSTITUTE, 'UTF-8' );
|
|
}
|
|
|
|
$out = '';
|
|
foreach ( $parts as $part ) {
|
|
if ($part === '') {
|
|
continue;
|
|
}
|
|
|
|
if ($part[0] !== '<') {
|
|
$out .= $part;
|
|
continue;
|
|
}
|
|
|
|
if (preg_match ( '/^<\s*(\/?)\s*([a-z0-9]+)\s*(\/?)\s*>$/i', $part, $m ) !== 1) {
|
|
$out .= htmlspecialchars ( $part, ENT_QUOTES | ENT_SUBSTITUTE, 'UTF-8' );
|
|
continue;
|
|
}
|
|
|
|
$isClose = ($m[1] === '/');
|
|
$tag = strtolower ( $m[2] );
|
|
$isSelfClose = ($m[3] === '/');
|
|
|
|
if (! isset ( $allow[$tag] )) {
|
|
$out .= htmlspecialchars ( $part, ENT_QUOTES | ENT_SUBSTITUTE, 'UTF-8' );
|
|
continue;
|
|
}
|
|
|
|
if ($isClose) {
|
|
if (isset ( $selfClosing[$tag] )) {
|
|
continue;
|
|
}
|
|
$out .= "</{$tag}>";
|
|
continue;
|
|
}
|
|
|
|
if ($isSelfClose && ! isset ( $selfClosing[$tag] )) {
|
|
$out .= "</{$tag}>";
|
|
continue;
|
|
}
|
|
|
|
if (isset ( $selfClosing[$tag] )) {
|
|
$out .= "<{$tag}>";
|
|
continue;
|
|
}
|
|
|
|
$out .= "<{$tag}>";
|
|
}
|
|
|
|
return $out;
|
|
}
|
|
function linkify(string $input): string {
|
|
$pattern = '@(http(s)?://[a-zA-Z0-9/\.\#\-\_]*)@';
|
|
return $output = preg_replace ( $pattern, '<a href="$1">$1</a>', $input );
|
|
}
|
|
function inStr(string $needle, string $haystack): bool {
|
|
if (strpos ( $haystack, $needle ) !== false) {
|
|
return true;
|
|
}
|
|
return false;
|
|
}
|
|
?>
|