Skip to content
Classroom

String Macros

Built-in functions for text manipulation and string operations.

String Functions

strlen(string)

Return the length of a string.

$length = strlen("hello");  // Returns 5
$len = strlen($variable);

strtolower(string), strtoupper(string)

Convert to lowercase or uppercase.

$lower = strtolower("HELLO");     // Returns "hello"
$upper = strtoupper("hello");     // Returns "HELLO"

trim(string)

Remove whitespace from both ends.

$cleaned = trim("  hello world  ");  // Returns "hello world"

substr(string, start, length)

Extract a substring.

$word = "hello";
$first_three = substr($word, 0, 3);     // Returns "hel"
$last_two = substr($word, -2);          // Returns "lo"

Find position of substring (returns -1 if not found).

$position = strpos("hello world", "world");  // Returns 6
$found = strpos("hello", "x");               // Returns -1

str_replace(search, replace, subject)

Replace all occurrences of a substring.

$text = "The cat is in the hat";
$new = str_replace("cat", "dog", $text);
// Returns "The dog is in the hat"

str_repeat(string, multiplier)

Repeat a string multiple times.

$dashes = str_repeat("-", 10);  // Returns "----------"
$pattern = str_repeat("ab", 3); // Returns "ababab"

implode(glue, array), join(glue, array)

Join array elements into a string with separator.

$items = array("apple", "banana", "orange");
$list = implode(", ", $items);  // Returns "apple, banana, orange"

explode(separator, string)

Split a string by separator into array.

$text = "apple, banana, orange";
$items = explode(", ", $text);
// Returns array("apple", "banana", "orange")

Text Generation

ucfirst(string), lcfirst(string)

Uppercase or lowercase the first character.

$capitalized = ucfirst("hello");  // Returns "Hello"
$lower = lcfirst("HELLO");        // Returns "hELLO"

str_pad(string, length, pad_string)

Pad string to specific length.

$number = str_pad("5", 3, "0", STR_PAD_LEFT);  // Returns "005"
$padded = str_pad("hello", 10, ".");          // Returns "hello....."

htmlspecialchars(string)

Escape HTML special characters (for safe display).

$safe = htmlspecialchars('<script>alert("XSS")</script>');
// Safe to display in HTML

Examples

Generate a Sentence

$name = "Alice";
$number = 5;

$sentence = ucfirst($name) . " has " . $number . " apples.";
// Returns "Alice has 5 apples."

Validate Text Length

$answer = "hello";

if (strlen($answer) > 10) {
    feedback = "Your answer is too long.";
} elseif (strlen($answer) < 2) {
    feedback = "Your answer is too short.";
}

Process Comma-Separated Input

$input = "red, blue, green";
$colors = explode(", ", $input);

$first_color = $colors[0];  // Returns "red"

Create Formatted Display

$values = array("10", "20", "30");
$formatted = "Values: " . implode(" | ", $values);
// Returns "Values: 10 | 20 | 30"

Case-Insensitive Comparison

$student_answer = "PaRiS";
$correct = "Paris";

if (strtolower($student_answer) == strtolower($correct)) {
    // Correct answer (case-insensitive match)
}

Tips

  • Use htmlspecialchars() when displaying user input in HTML
  • Be careful with string indices: Arrays are zero-indexed
  • Check strpos() against -1: It returns false (0-like) for not found
  • Use implode/explode for converting between arrays and strings

See Also