Course Content
About Lesson

PHP String Functions

Returns the length of a string (number of characters, including spaces).

PHP
<?php
$text = "Hello World!";
echo "Length: " . strlen($text);
?>

Output: Length: 12

Replaces every occurrence of a word or character with another.

PHP
<?php
$text = "I like apples.";
$newText = str_replace("apples", "mangoes", $text);

echo $newText;
?>

Output: I like mangoes.

Returns a portion of a string.

PHP
<?php
$text = "Hello World!";
echo substr($text, 0, 5);   // From index 0 → 5 characters
echo "<br>";
echo substr($text, 6);      // From index 6 till end
?>

Output: Array ( [0] => red [1] => blue [2] => green [3] => yellow )

Finds where the substring first appears in the string.

PHP
<?php
$text = "Hello World!";
$pos = strpos($text, "World");

if ($pos !== false) {
    echo "Found at position: " . $pos;
}
?>

Output: Found at position: 6