SyntaxStudy
Sign Up
PHP PHP String Functions
PHP Beginner 1 min read

PHP String Functions

PHP String Functions

FunctionDescription
strlen($s)Length
strtoupper($s)Uppercase
strtolower($s)Lowercase
trim($s)Remove whitespace
str_replace($a,$b,$s)Replace
strpos($s,$find)Position of substring
substr($s,$start,$len)Substring
str_contains($s,$sub)Contains? (PHP 8)
explode($sep,$s)String to array
implode($sep,$arr)Array to string
sprintf($fmt,...)Format string
Example
<?php
$str = "  Hello, World!  ";
echo strlen($str);               // 17
echo trim($str);                 // "Hello, World!"
echo strtoupper($str);           // "  HELLO, WORLD!  "
echo str_replace("World","PHP",$str); // replaces

$parts = explode(",", "a,b,c");  // ["a","b","c"]
echo implode(" | ", $parts);    // "a | b | c"

echo sprintf("%.2f", 3.14159);  // "3.14"
echo str_pad("42", 5, "0", STR_PAD_LEFT); // "00042"
?>