polyfills for php8's str_starts_with and str_ends_with
This commit is contained in:
parent
4ac9ab2ad6
commit
c783ff0e8d
4 changed files with 21 additions and 18 deletions
|
@ -232,7 +232,7 @@ class Image
|
||||||
// total number of images in the DB
|
// total number of images in the DB
|
||||||
$total = self::count_total_images();
|
$total = self::count_total_images();
|
||||||
} elseif (SPEED_HAX && $tag_count === 1 && !preg_match("/[:=><\*\?]/", $tags[0])) {
|
} elseif (SPEED_HAX && $tag_count === 1 && !preg_match("/[:=><\*\?]/", $tags[0])) {
|
||||||
if (!startsWith($tags[0], "-")) {
|
if (!str_starts_with($tags[0], "-")) {
|
||||||
// one tag - we can look that up directly
|
// one tag - we can look that up directly
|
||||||
$total = self::count_tag($tags[0]);
|
$total = self::count_tag($tags[0]);
|
||||||
} else {
|
} else {
|
||||||
|
@ -533,7 +533,7 @@ class Image
|
||||||
$image_link = $config->get_string($template);
|
$image_link = $config->get_string($template);
|
||||||
|
|
||||||
if (!empty($image_link)) {
|
if (!empty($image_link)) {
|
||||||
if (!(strpos($image_link, "://") > 0) && !startsWith($image_link, "/")) {
|
if (!(strpos($image_link, "://") > 0) && !str_starts_with($image_link, "/")) {
|
||||||
$image_link = make_link($image_link);
|
$image_link = make_link($image_link);
|
||||||
}
|
}
|
||||||
$chosen = $image_link;
|
$chosen = $image_link;
|
||||||
|
@ -723,7 +723,7 @@ class Image
|
||||||
$page->flash("Can't set a tag longer than 255 characters");
|
$page->flash("Can't set a tag longer than 255 characters");
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
if (startsWith($tag, "-")) {
|
if (str_starts_with($tag, "-")) {
|
||||||
$page->flash("Can't set a tag which starts with a minus");
|
$page->flash("Can't set a tag which starts with a minus");
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
|
@ -341,17 +341,20 @@ function unparse_url($parsed_url)
|
||||||
return "$scheme$user$pass$host$port$path$query$fragment";
|
return "$scheme$user$pass$host$port$path$query$fragment";
|
||||||
}
|
}
|
||||||
|
|
||||||
function startsWith(string $haystack, string $needle): bool
|
# finally in the core library starting from php8
|
||||||
{
|
if (!function_exists('str_starts_with')) {
|
||||||
|
function str_starts_with(string $haystack, string $needle): bool
|
||||||
|
{
|
||||||
$length = strlen($needle);
|
$length = strlen($needle);
|
||||||
return (substr($haystack, 0, $length) === $needle);
|
return (substr($haystack, 0, $length) === $needle);
|
||||||
}
|
}
|
||||||
|
|
||||||
function endsWith(string $haystack, string $needle): bool
|
function str_ends_with(string $haystack, string $needle): bool
|
||||||
{
|
{
|
||||||
$length = strlen($needle);
|
$length = strlen($needle);
|
||||||
$start = $length * -1; //negative
|
$start = $length * -1; //negative
|
||||||
return (substr($haystack, $start) === $needle);
|
return (substr($haystack, $start) === $needle);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *\
|
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *\
|
||||||
|
|
|
@ -46,9 +46,9 @@ function contact_link(): ?string
|
||||||
}
|
}
|
||||||
|
|
||||||
if (
|
if (
|
||||||
startsWith($text, "http:") ||
|
str_starts_with($text, "http:") ||
|
||||||
startsWith($text, "https:") ||
|
str_starts_with($text, "https:") ||
|
||||||
startsWith($text, "mailto:")
|
str_starts_with($text, "mailto:")
|
||||||
) {
|
) {
|
||||||
return $text;
|
return $text;
|
||||||
}
|
}
|
||||||
|
|
|
@ -109,13 +109,13 @@ class TagEditTheme extends Themelet
|
||||||
protected function format_source(string $source=null): string
|
protected function format_source(string $source=null): string
|
||||||
{
|
{
|
||||||
if (!empty($source)) {
|
if (!empty($source)) {
|
||||||
if (!startsWith($source, "http://") && !startsWith($source, "https://")) {
|
if (!str_starts_with($source, "http://") && !str_starts_with($source, "https://")) {
|
||||||
$source = "http://" . $source;
|
$source = "http://" . $source;
|
||||||
}
|
}
|
||||||
$proto_domain = explode("://", $source);
|
$proto_domain = explode("://", $source);
|
||||||
$h_source = html_escape($proto_domain[1]);
|
$h_source = html_escape($proto_domain[1]);
|
||||||
$u_source = html_escape($source);
|
$u_source = html_escape($source);
|
||||||
if (endsWith($h_source, "/")) {
|
if (str_ends_with($h_source, "/")) {
|
||||||
$h_source = substr($h_source, 0, -1);
|
$h_source = substr($h_source, 0, -1);
|
||||||
}
|
}
|
||||||
return "<a href='$u_source'>$h_source</a>";
|
return "<a href='$u_source'>$h_source</a>";
|
||||||
|
|
Reference in a new issue