String functions CODE
Function
strlen()
returns the length of the string
echo strlen("Hello world!")."\n"; // 12
Function
strrev()
returns the Reverse of the string
echo strrev("Hello world!")."\n"; // !dlrow olleH
Function
strpos()
tries to find the second parameter and returns its index
echo strpos("Hello world!", "world")."\n"; // 6
Function
str_replace()
replaces the word "Hello" with "Hi"
echo str_replace("Hello", "Hi", "Hello there")."\n"; // Hi there
Function
substr()
returns the substring between given indexes
echo substr("Hello world", 6)."\n"; // world
echo substr("Hello world", 6, 3)."\n"; // wor
echo substr("Hello world", -5)."\n"; // world
echo substr("Hello world", -4, 2)."\n"; // or
Function
trim()
removes whitespaces from sides of the string
echo trim("   Hello World  ").".\n"; // Hello World.
// Removes whitespaces from the right end
echo rtrim("   Hello World  ")."\n"; //    Hello World
// Removes Exclamation mark from the right end
echo rtrim("Hello World!", "!")."\n"; // Hello World
Function
strtoupper()
converts the string to upper case
echo strtoupper("Hello World")."\n";
Function
strtolower()
converts the string to lower case
echo strtolower("Hello World")."\n";
Function
explode()
breaks the string by its delimiter
print_r (explode(" ", "It's the first day of the course!"));
Function
implode()
joins the array element based on the delimiter and returns the string
echo implode(" ", ['Hello', 'World']);
by Valeri Tandilashvili
2 years ago
PHP
functions
2
Pro tip: use ```triple backticks around text``` to write in code fences