Skip to content
PHP

PHP의 문자열 함수

PHP에서 substr, replace, explode, sprintf로 문자열 조작.

#string#beginner

Code

php
<?php

$str = "Hello, World";

// Length and case
echo strlen($str);          // 12
echo strtoupper($str);      // HELLO, WORLD
echo strtolower($str);

// Substring and replace
echo substr($str, 0, 5);    // Hello
echo str_replace("World", "PHP", $str);

// Search
$pos = strpos($str, "World");
if ($pos !== false) {
    echo "Found at $pos";
}

// Split and join
$parts = explode(", ", $str);
$joined = implode(" | ", $parts);

// Format
$formatted = sprintf("Name: %s, Age: %d", "Alice", 30);

// Trim
echo trim("  hello  ");     // hello