Coder Social home page Coder Social logo

php-utils's Introduction

php-utils

A set of handy php functions

Usage

All Util::method() are also exposed as global functions. In order to avoid conflicts with already declared functions with the same name, the global functions are wrapped in an if (! function_exists('fn_name')) { block.

// StringUtils

normalize_whitespace(...$args)
capitalize(...$args)
instance_or_default(...$args)
slugify(...$args)
words(...$args)
is_all_upper_case(...$args)
pascal_case(...$args)
camel_case(...$args)
snake_case(...$args)
kebab_case(...$args)
excel_date_to_php_date(...$args)
array_flatten(...$args)
array_keys_transform(...$args)
array_unique_value(...$args)
is_url(...$args)
is_valid_url(...$args)

String Utils

  1. normalizeWhitespace(string $str = '')

Removes extra whitespace and trims the string.

$str = "  This string \n has \t extra  \v   spaces .   ";
$normalizedStr = Utils::normalizeWhitespace($str);
// $normalizedStr = "This string has extra spaces .";
  1. capitalize(string $str = '')

Capitalizes first letter of each word, lowercase rest.

$str = "this SENTENCE needs CAPITALIZATION!";
$capitalizedStr = Utils::capitalize($str);
// $capitalizedStr = "This Sentence Needs Capitalization";
  1. slugify(string $str = '', bool $toLower = true)

Converts to URL-friendly format with dashes/underscores. Optional toLower controls lowercase conversion.

$str = "My Awesome Product Name!";
$slug = Utils::slugify($str);
// $slug = "my-awesome-product-name";

$slugWithUppercase = Utils::slugify($str, false);
// $slugWithUppercase = "My-Awesome-Product-Name";
  1. words(string $str = '')

Splits into an array of words, preserving capitalization for single words.

$str = "This is a sentence with multiple words.";
$words = Utils::words($str);
// $words = ["This", "Is", "A", "Sentence", "With", "Multiple", "Words"]; 
  1. isAllUpperCase(string $str)

Checks if all characters are uppercase.

$str = "ALL UPPERCASE";
$isAllUpper = Utils::isAllUpperCase($str); // true

$str = "Not All Uppercase";
$isAllUpper = Utils::isAllUpperCase($str); // false
  1. pascalCase(string $str = '')

Converts to PascalCase (all words start with uppercase).

$str = "this is a string";
$pascalCase = Utils::pascalCase($str);
// $pascalCase = "ThisIsString";
  1. camelCase(string $str = '')

Converts to camelCase (first word lowercase, others uppercase).

$str = "This is a string";
$camelCase = Utils::camelCase($str);
// $camelCase = "thisIsAString";
  1. snakeCase(string $str = '')

Converts to snake_case (lowercase with underscores).

$str = "This is a string";
$snakeCase = Utils::snakeCase($str);
// $snakeCase = "this_is_a_string";
  1. kebabCase(string $str)

Converts to kebab-case (lowercase with hyphens).

$str = "This is a string";
$kebabCase = Utils::kebabCase($str);
// $kebabCase = "this-is-a-string";
  1. isUrl(string $url)

Basic URL validation using built-in functions.

$url = "https://www.example.com";
$isUrlValid = Utils::isUrl($url); // true

$url = "invalid-url";
$isUrlValid = Utils::isUrl($url); // false
  1. isValidUrl(string $url)

Performs more rigorous URL validation with parsing and hostname checks.

$url = "https://www.example.com";
$isValidUrl = Utils::isValidUrl($url); // true

$url = "invalid-url";
$isValidUrl = Utils::isValidUrl($url); // false

php-utils's People

Contributors

parables avatar

Stargazers

 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.