Explore the realm of PHP string manipulation with essential functions that unveil the magic within. Transmute UTF-8 characters into ASCII, wield camel case conversion, validate substring presence, and crown strings with precision. Immerse in practical demonstrations to ascend your PHP prowess today.
In this article, we delve into 10 indispensable PHP string functions that streamline text manipulation tasks. From transforming UTF-8 characters to ASCII to generating random alphanumeric strings, these functions such as Str::camel, Str::plural, and Str::studly empower developers to handle strings with ease. Whether you're checking for substring presence with Str::contains or transforming strings to snake case using Str::snake, these tools are essential for crafting efficient and maintainable code. Dive into this guide and elevate your PHP string handling skills
Below is an example of how you could use some of these string manipulation functions in PHP code:
<?php
// Example string
$value = "hello world";
// Transliterate a UTF-8 value to ASCII
$asciiValue = Str::ascii($value);
echo "ASCII Value: $asciiValue\n";
// Convert string to camel case
$camelCaseValue = Str::camel($value);
echo "Camel Case Value: $camelCaseValue\n";
// Check if a string contains another string
$haystack = "This is a test string";
$needle = "test";
if(Str::contains($haystack, $needle)) {
echo "The string contains '$needle'\n";
} else {
echo "The string does not contain '$needle'\n";
}
// Check if a string ends with certain characters
$haystack = "This is a test string";
$needles = ["string", "world"];
if(Str::endsWith($haystack, $needles)) {
echo "The string ends with one of the needles\n";
} else {
echo "The string does not end with any of the needles\n";
}
// Cap a string with a single instance of a given value
$value = "hello";
$cap = "!";
$finishedValue = Str::finish($value, $cap);
echo "Capped Value: $finishedValue\n";
// Check if a string matches a given pattern
$pattern = '/hello/';
$value = "hello world";
if(Str::is($pattern, $value)) {
echo "The string matches the pattern\n";
} else {
echo "The string does not match the pattern\n";
}
// Get the length of a string
$value = "hello world";
$length = Str::length($value);
echo "Length of the string: $length\n";
// Limit the length of a string
$value = "This is a long string that needs to be shortened";
$shortenedValue = Str::limit($value, 10);
echo "Shortened Value: $shortenedValue\n";
// Convert a string to lowercase
$value = "Hello World";
$lowercaseValue = Str::lower($value);
echo "Lowercase Value: $lowercaseValue\n";
// Extract a specific number of words from a string
$value = "This is a long sentence that needs to be shortened";
$words = Str::words($value, 5);
echo "Extracted Words: $words\n";
// Generate a random alphanumeric string
$randomString = Str::random(8);
echo "Random String: $randomString\n";
// Convert a string to uppercase
$value = "hello world";
$uppercaseValue = Str::upper($value);
echo "Uppercase Value: $uppercaseValue\n";
// Convert a string to title case
$value = "hello world";
$titleCaseValue = Str::title($value);
echo "Title Case Value: $titleCaseValue\n";
// Convert a string to singular form
$value = "apples";
$singularValue = Str::singular($value);
echo "Singular Value: $singularValue\n";
// Generate a slug from a string
$title = "This is a sample title";
$slug = Str::slug($title);
echo "Slug: $slug\n";
// Convert a string to snake case
$value = "helloWorld";
$snakeCaseValue = Str::snake($value);
echo "Snake Case Value: $snakeCaseValue\n";
// Check if a string starts with certain characters
$haystack = "This is a test string";
$needles = ["This", "Hello"];
if(Str::startsWith($haystack, $needles)) {
echo "The string starts with one of the needles\n";
} else {
echo "The string does not start with any of the needles\n";
}
// Convert a value to studly caps case
$value = "hello_world";
$studlyValue = Str::studly($value);
echo "Studly Case Value: $studlyValue\n";
// Add a custom macro to the Str class
Str::macro('reverse', function($value) {
return strrev($value);
});
// Example of using a custom macro
$value = "hello world";
$reversedValue = Str::reverse($value);
echo "Reversed Value: $reversedValue\n";
?>