30 seconds of PHP

String

Click on a snippet's name to view its code.

countVowels

Returns number of vowels in the provided string.

Use a regular expression to count the number of vowels (a, e, i, o and ua) in a string.

decapitalize

Decapitalizes the first letter of a string.

Decapitalizes the first letter of the string and then adds it with rest of the string. Omit the $upperRest parameter to keep the rest of the string intact, or set it to true to convert to uppercase.

endsWith

Checks if a string is ends with a given substring.

Use strrpos() in combination with strlen to find the position of $needle in $haystack.

firstStringBetween

Returns the first string there is between the strings from the parameter $start and $end.

Use trim() and strstr() to find the string contained between $start and $end.

isAnagram

Compare two strings and returns true if both strings are anagram, false otherwise.

Use count_chars() to compare $string1 and $string2.

isContains

Check if a word / substring exists in a given string input.

Using strpos() to find the position of the first occurrence of a substring in a string.

isLowerCase

Returns true if the given string is lower case, false otherwise.

Convert the given string to lower case, using strtolower and compare it to the original.

isUpperCase

Returns true if the given string is upper case, false otherwise.

Convert the given string to upper case, using strtoupper and compare it to the original.

palindrome

Returns true if the given string is a palindrome, false otherwise.

Check if the value of strrev($string) is equal to the passed $string.

shorten

Returns a shortened string.

Use mb_strlen(), mb_substr() and rtrim() to shorten a string to a give number of characters.

slugify

Converts a string to a URL-friendly slug.

Uses preg_replace() to replace invalid chars with dashes, iconv() to convert the text to ASCII, strtolower() and trim() to convert to lowercase and remove extra whitespace.

startsWith

Check if a string starts with a given substring.

Use strpos() to find the position of $needle in $haystack.