Files
futurewalker/app/Helpers/Global/string_helper.php
2023-11-28 19:32:42 +08:00

241 lines
5.9 KiB
PHP

<?php
use GrahamCampbell\Markdown\Facades\Markdown;
use Illuminate\Support\Str;
if (! function_exists('get_country_names')) {
function get_country_names($lowercase = false) {
$countryCodes = config('platform.country_codes');
$countryNames = [];
foreach ($countryCodes as $code => $country) {
$name = $country['name'];
if ($lowercase) {
$name = strtolower($name);
}
$countryNames[] = $name;
}
return $countryNames;
}
}
if (! function_exists('is_valid_url')) {
function is_valid_url($url)
{
// Check if the URL is a valid full URL
if (filter_var($url, FILTER_VALIDATE_URL)) {
return true;
}
// Check if it's a valid relative URL
// Modify this regex as needed for your specific URL formats
if (preg_match('/^\/[\w\/_.-]+(\.[\w]+)?$/i', $url)) {
return true;
}
return false;
}
}
if (! function_exists('epoch_now_timestamp')) {
function epoch_now_timestamp()
{
return (int) round(microtime(true) * 1000);
}
}
if (! function_exists('read_duration')) {
function read_duration($text)
{
return Str::readDuration($text);
}
}
if (! function_exists('remove_newline')) {
function remove_newline($string)
{
return preg_replace('/\s+/', ' ', trim($string));
}
}
if (! function_exists('plain_text')) {
function plain_text($content)
{
return trim(preg_replace('/\s\s+/', ' ', strip_tags($content)));
}
}
if (! function_exists('markdown_to_plaintext')) {
function markdown_to_plaintext($markdown)
{
// Headers
$markdown = preg_replace('/^#.*$/m', '', $markdown);
// Links and images
$markdown = preg_replace('/\[(.*?)\]\(.*?\)/', '$1', $markdown);
// Bold and italic
$markdown = str_replace(['**', '__', '*', '_'], '', $markdown);
// Ordered and unordered lists
$markdown = preg_replace('/^[-\*].*$/m', '', $markdown);
// Horizontal line
$markdown = str_replace(['---', '***', '- - -', '* * *'], '', $markdown);
// Inline code and code blocks
$markdown = preg_replace('/`.*?`/', '', $markdown);
$markdown = preg_replace('/```[\s\S]*?```/', '', $markdown);
// Blockquotes
$markdown = preg_replace('/^>.*$/m', '', $markdown);
// Remove multiple spaces, leading and trailing spaces
$plaintext = trim(preg_replace('/\s+/', ' ', $markdown));
return $plaintext;
}
}
if (! function_exists('markdown_min_read')) {
function markdown_min_read($markdown)
{
return read_duration(plain_text(Markdown::convert($markdown)->getContent()));
}
}
if (! function_exists('min_read')) {
function min_read($string)
{
return read_duration(plain_text($string));
}
}
if (! function_exists('unslug')) {
function unslug($slug, $delimiter = '-')
{
return ucwords(str_replace($delimiter, ' ', $slug));
}
}
if (! function_exists('str_slug')) {
function str_slug($string, $delimiter = '-')
{
return Str::of(trim($string))->slug($delimiter);
}
}
if (! function_exists('str_first_sentence')) {
function str_first_sentence($str)
{
// Split the string at ., !, or ? but include these characters in the match
$sentences = preg_split('/([.!?])\s/', $str, 2, PREG_SPLIT_DELIM_CAPTURE);
// Check if we have captured the first sentence and its punctuation
if (isset($sentences[0]) && isset($sentences[1])) {
return trim($sentences[0].$sentences[1]);
}
// If no sentence ending found, return the whole string
return $str;
}
}
if (! function_exists('is_empty')) {
/**
* A better function to check if a value is empty or null. Strings, arrays, and Objects are supported.
*
* @param mixed $value
*/
function is_empty($value): bool
{
if (is_null($value)) {
return true;
}
if (is_string($value)) {
if ($value === '') {
return true;
}
}
if (is_array($value)) {
if (count($value) === 0) {
return true;
}
}
if (is_object($value)) {
$value = (array) $value;
if (count($value) === 0) {
return true;
}
}
return false;
}
}
if (! function_exists('get_domain_from_url')) {
function get_domain_from_url($url)
{
$parse = parse_url($url);
// Check if 'host' key exists in the parsed URL array
if (!isset($parse['host'])) {
return null; // or you can throw an exception or handle this case as per your requirement
}
$host = $parse['host'];
// Check if the domain starts with 'www.' and remove it
if (substr($host, 0, 4) === 'www.') {
$host = substr($host, 4);
}
return $host;
}
}
if (! function_exists('get_country_name_by_iso')) {
function get_country_name_by_iso($country_iso)
{
if (! is_empty($country_iso)) {
$country_iso = strtoupper($country_iso);
try {
return config("platform.country_codes.{$country_iso}")['name'];
} catch (\Exception $e) {
}
}
return 'International';
}
}
if (! function_exists('get_country_emoji_by_iso')) {
function get_country_emoji_by_iso($country_iso)
{
if (! is_empty($country_iso)) {
$country_iso = strtoupper($country_iso);
try {
return config("platform.country_codes.{$country_iso}")['emoji'];
} catch (\Exception $e) {
}
}
return '🌎';
}
}
if (! function_exists('str_random')) {
function str_random($length = 10)
{
return Str::random($length);
}
}