clock  Mon - Sun 8.00 AM - 8.00 PM
play store

String Functions In Laravel

writter  Sumit Dey Sarkar
Date  22 Mar 2023
Language  Laravel
String Functions In Laravel

String Functions In Laravel

In this tutorial we will learn string function in laravel.


String functions In laravel

Laravel is a popular PHP web framework that provides a variety of built-in functions and methods for working with strings.

Here are some commonly used string functions in Laravel:


1) str_replace()

This function is used to replace a substring with another substring within a string.

Syntax: str_replace($search, $replace, $subject)

Example: $new_string = str_replace('old', 'new', 'This is an old string');


2) strtolower()

This function is used to convert a string to lowercase.

Syntax: strtolower($string)

Example: $lowercase_string = strtolower('THIS IS AN UPPERCASE STRING');


3) strtoupper()

This function is used to convert a string to uppercase.

Syntax: strtoupper($string)

Example: $uppercase_string = strtoupper('this is a lowercase string');


4) ucfirst()

This function is used to capitalize the first letter of a string.

Syntax: ucfirst($string)

Example: $capitalized_string = ucfirst('this is a sentence');


5) ucwords()

This function is used to capitalize the first letter of each word in a string.

Syntax: ucwords($string)

Example: $capitalized_words_string = ucwords('this is a sentence');


6) str_limit()

This function is used to limit the number of characters in a string.

Syntax: str_limit($string, $limit)

Example: $limited_string = str_limit('This is a long string', 10);


7) str_contains()

This function is used to check if a string contains a specific substring.

Syntax: str_contains($string, $substring)

Example: $contains = str_contains('This is a string', 'is a');


8) str_starts_with()

This function is used to check if a string starts with a specific substring.

Syntax: str_starts_with($string, $substring)

Example: $starts_with = str_starts_with('This is a string', 'This');


9) str_ends_with()

This function is used to check if a string ends with a specific substring.

Syntax: str_ends_with($string, $substring)

Example: $ends_with = str_ends_with('This is a string', 'string');


10) str_plural()

This function is used to pluralize a string based on the count.

Syntax: str_plural($string, $count)

Example: $pluralized_string = str_plural('apple', 3);


11) str_singular()

This function is used to singularize a string.

Syntax: str_singular($string)

Example: $singular_string = str_singular('apples');


12) str_random()

This function is used to generate a random string of a specified length.

Syntax: str_random($length)

Example: $random_string = str_random(10);


13) str_slug()

This function is used to convert a string to a URL friendly slug.

Syntax: str_slug($string)

Example: $slug = str_slug('This is a URL friendly string');


14) str_replace_array()

This function is used to replace multiple substrings with an array of replacements in a string.

Syntax: str_replace_array($search, $replace, $subject)

Example: $new_string = str_replace_array(['old', 'new'], ['new', 'old'], 'This is an old string');


15) str_contains_all()

This function is used to check if a string contains all of the given substrings.

Syntax: str_contains_all($string, $substrings)

Example: $contains_all = str_contains_all('This is a string', ['is', 'string']);


16) str_before()

This function is used to get the part of a string before a specific substring.

Syntax: str_before($string, $substring)

Example: $before_string = str_before('This is a string', 'is');


17) str_after()

This function is used to get the part of a string after a specific substring.

Syntax: str_after($string, $substring)

Example: $after_string = str_after('This is a string', 'a ');


18) str_limit_words()

This function is used to limit the number of words in a string.

Syntax: str_limit_words($string, $limit)

Example: $limited_string = str_limit_words('This is a long string with many words', 3);


19) str_pad()

This function is used to pad a string with a specific character or substring to a specified length.

Syntax: str_pad($string, $length, $pad_string, $pad_type)

Example: $padded_string = str_pad('123', 5, '0', STR_PAD_LEFT);


20) str_shuffle()

This function is used to shuffle the characters in a string.

Syntax: str_shuffle($string)

Example: $shuffled_string = str_shuffle('This is a string');

Comments 0

Leave a comment

Coursera, Codeacademy, Udacity, W3Schools, Udemy, Alison, TheNewBoston, edX, P.S.Codewars,Freecodecamp, Managing technical debt blog, Scrimba, Codepen, Codepen/challenges, The Odin Project, htmlreference.​io, cssreference.​io, Frontend Mentor, Dev Challenges, MDN, Code Mentor, Coding Dojo, CSS Battle, Codier, Ace Frontend, Can I Use, CSS Tricks, 30 Seconds of Code,tutorialspoint, Neumorphism, Shaddows Brumm, Fancy Border Radius, Glow Generator, Clothoid Corners, Glassmorphism, Clipy, CSS Filters, Base64 Image, Quantity Queries, Animations, Cubic-Bezier, Keyframes, Wait Animate, Transition.Style, graphic design, web design, website design, website builder, web developer, web designer, webdesign, ecommerce website, web design company, website creator, website designer, responsive web design, web development company, best website design, web design software, web page design, build a website, web developer salary, design website, web design courses, how to design a website, web design inspiration, website layout, web designer salary, web application development, ecommerce website design, web agency, software development company, web design tutorial, web programming, design company, website design templates, what is web designing, web developer jobs, website developer, web design agency, freelance web developer, web design services, freelance web designer, graphic design websites, web solutions, ecommerce website development, free website design, web development courses, webdev, web developers, web development tools, website design services, developpeur web, web design london, website design ideas, web designing and programming, design a website, web design and development, web dev, web development services, homepage design, best designed websites, cheap website design, learn web design, web design templates, web design tools, web design jobs, website design inspiration, web design india, flash website, website developers, designer websites, website services, website design cost, good website design, site design, simple website design, cool website designs, modern website design, graphic designer websites, webcode, best web design software, website making, free web design software, mobile website design, learn web development, front end web developer, how to become a web developer, web developer portfolio, web development company in india, python web development, web development tutorial, website company, website design and development, web company, webdesigning, professional website design, affordable web design, best web design company, creative web design, top website designs, website design pricing, web developer tools, how to develop a website