PHP Strings · Chapter 7.1 · Function Deep Dive

PHP String Functions
strlen · strtoupper · strtolower · trim · ucwords

पाँच सबसे ज़रूरी PHP String Functions की पूरी जानकारी — Syntax, Parameters, Return Value, Real World Examples, और Common Mistakes।

📏 strlen() 🔠 strtoupper() 🔡 strtolower() ✂️ trim() 🅰️ ucwords()
5Functions इस chapter में
stringसभी string return करते हैं
dailyहर PHP project में use
PHP 4+सभी functions available

📋 इस Article में क्या-क्या है

  1. strlen() — String Length
  2. strtoupper() — UPPERCASE
  3. strtolower() — lowercase
  4. trim() — Whitespace हटाना
  5. ucwords() — Title Case
  6. Comparison Table
  7. Combined Real World Example
1
strlen() — String की Length
str
len

strlen()

String में कितने characters हैं — यह count करके return करता है। Password validation, username length check, character limit — हर जगह use होता है।

PHP 4+ Returns: int Case Sensitive
int strlen( string $string )
ParameterTypeज़रूरी?Description
$stringstringRequiredजिस string की length निकालनी है
ReturnintCharacters की count। Empty string पर 0।
strlen() — BASIC EXAMPLES
<?php
// Basic use
echo strlen("Hello"); // 5
echo strlen("HindiNotes"); // 10
echo strlen(""); // 0 (empty string)
echo strlen("Hello World"); // 11 (space भी count होती है)

// Variable के साथ
$naam = "Rahul Kumar";
echo strlen($naam); // 11
?>
Real World Use Cases:
🔒
Password Validation

Password कम से कम 8 characters का है?

👤
Username Check

Username 3-20 characters के बीच है?

📝
Tweet/Post Limit

280 characters से ज़्यादा नहीं

✂️
Preview Text

100 chars से लंबा text truncate करो

strlen() — REAL WORLD EXAMPLES
<?php
// 1. Password validation
$password = "mypass";
if (strlen($password) < 8) {
  echo "❌ Password कम से कम 8 characters का होना चाहिए";
} elseif (strlen($password) > 20) {
  echo "❌ Password 20 characters से ज़्यादा नहीं होना चाहिए";
} else {
  echo "✅ Valid password";
}

// 2. Blog preview — 100 chars + "..."
$content = "PHP एक open source server-side scripting language है जिसका use web development में होता है।";
if (strlen($content) > 50) {
  echo substr($content, 0, 50) . "...";
}

// 3. Remaining characters count (like Twitter)
$tweet = "PHP सीखना बहुत आसान है!";
$maxLimit = 280;
$remaining = $maxLimit - strlen($tweet);
echo "बचे हुए characters: " . $remaining;
?>
⚠️ Multibyte Warning: strlen() bytes count करता है, characters नहीं। Hindi/Urdu जैसी multibyte languages में एक character = 3 bytes। इसलिए strlen("नमस्ते") → 18 आएगा (6 नहीं)। Hindi strings के लिए mb_strlen("नमस्ते") → 6 use करो।

2
strtoupper() — सब UPPERCASE करना
STR
UP

strtoupper()

String के सभी letters को UPPERCASE (capital) में convert करता है। Original string नहीं बदलती — नई string return होती है।

PHP 4+ Returns: string Non-destructive
string strtoupper( string $string )
strtoupper() — EXAMPLES
<?php
echo strtoupper("hello world"); // HELLO WORLD
echo strtoupper("rahul kumar"); // RAHUL KUMAR
echo strtoupper("php 8.3"); // PHP 8.3 (numbers same रहते हैं)
echo strtoupper("Hello123!"); // HELLO123! (special chars same)

// Original string नहीं बदलती
$naam = "rahul";
$upper = strtoupper($naam);
echo $naam; // rahul (unchanged)
echo $upper; // RAHUL (new string)
?>
strtoupper() — REAL WORLD EXAMPLES
<?php
// 1. Country code uppercase बनाना
$country = "india";
echo strtoupper($country); // INDIA

// 2. Case-insensitive comparison
$input = "yes";
$answer = "YES";
if (strtoupper($input) === $answer) {
  echo "✅ User ने yes दिया";
}

// 3. Heading को UPPERCASE display करना
$title = "php tutorial in hindi";
echo "<h1>" . strtoupper($title) . "</h1>";
// <h1>PHP TUTORIAL IN HINDI</h1>

// 4. Constant-style string बनाना
$key = "database_host";
echo strtoupper($key); // DATABASE_HOST
?>
💡 Pro Tip: Case-insensitive comparison के लिए दोनों strings को strtoupper() या strtolower() करके compare करो — यह user input handle करने का सही तरीका है।

3
strtolower() — सब lowercase करना
str
low

strtolower()

String के सभी letters को lowercase (small) में convert करता है। Email normalize करना, username standardize करना — daily use होता है।

PHP 4+ Returns: string Most used function
string strtolower( string $string )
strtolower() — EXAMPLES
<?php
echo strtolower("HELLO WORLD"); // hello world
echo strtolower("RAHUL KUMAR"); // rahul kumar
echo strtolower("PHP 8.3"); // php 8.3
echo strtolower("HeLLo WoRLd"); // hello world
?>
strtolower() — REAL WORLD EXAMPLES
<?php
// 1. Email normalize करना — सबसे common use!
$email = "Rahul@Gmail.COM";
$email = strtolower(trim($email));
echo $email; // rahul@gmail.com

// 2. URL slug बनाना
$title = "PHP Tutorial In Hindi";
$slug = strtolower(str_replace(" ", "-", $title));
echo $slug; // php-tutorial-in-hindi

// 3. Username lowercase बनाना
$username = "RaHuLkUmAr";
echo strtolower($username); // rahulkumar

// 4. File extension check
$filename = "Photo.JPG";
$extension = strtolower(pathinfo($filename, PATHINFO_EXTENSION));
if (in_array($extension, ['jpg', 'jpeg', 'png'])) {
  echo "✅ Valid image";
}
?>

✅ हमेशा email lowercase करो

// Registration में
$email = strtolower(
  trim($_POST['email'])
);
// Database में store करो

❌ बिना normalize किए store मत करो

// "Rahul@Gmail.COM" store होगा
// Login पर "rahul@gmail.com"
// Match नहीं होगा — Bug!
$email = $_POST['email'];

4
trim() — Extra Spaces हटाना
trim
()

trim()

String के शुरू और अंत से whitespace (spaces, tabs, newlines) हटाता है। User form input से extra spaces हटाना — यह सबसे ज़रूरी function है।

PHP 4+ Returns: string Form Input Must
string trim( string $string, string $characters = " \n\r\t\v\x00" )
ParameterTypeज़रूरी?Description
$stringstringRequiredजिस string को trim करना है
$charactersstringOptionalकौन से characters हटाने हैं। Default: spaces, \n, \r, \t
ReturnstringTrimmed string
trim() — BASIC EXAMPLES
<?php
// Basic trim — दोनों sides से spaces हटाओ
echo trim(" Hello "); // "Hello"
echo trim(" Rahul Kumar "); // "Rahul Kumar"
echo trim("\n\tHello\n"); // "Hello" (newline, tab भी हटता है)

// ltrim — सिर्फ left (शुरू) से
echo ltrim(" Hello "); // "Hello " (right spaces रहेंगे)

// rtrim — सिर्फ right (अंत) से
echo rtrim(" Hello "); // " Hello" (left spaces रहेंगे)

// Custom characters trim करना
echo trim("***Hello***", "*"); // "Hello"
echo trim("/path/to/page/", "/"); // "path/to/page"
?>
trim() — REAL WORLD EXAMPLES
<?php
// 1. Form input clean करना — हमेशा करो!
$naam = trim($_POST['naam'] ?? '');
$email = trim($_POST['email'] ?? '');

// 2. Empty check — trim के बाद करो
$input = " "; // user ने सिर्फ spaces दिए
if (trim($input) === '') {
  echo "❌ Field खाली नहीं छोड़ सकते";
}

// 3. URL path normalize करना
$path = "/home/user/photos/";
echo trim($path, "/"); // "home/user/photos"

// 4. Database query से आई values clean करना
$dbValue = " Rahul Kumar\n";
echo trim($dbValue); // "Rahul Kumar"

// 5. Chain करना — trim + strtolower
$email = strtolower(trim(" Admin@Site.COM "));
echo $email; // admin@site.com
?>
✅ Golden Rule: हर form input को trim() से ज़रूर process करो — users अक्सर accidentally spaces type कर देते हैं। "rahul " और "rahul" database में अलग store होते हैं लेकिन display में same लगते हैं।

5
ucwords() — हर Word का पहला Letter Capital
Uc
Wrd

ucwords()

String में हर word का पहला letter uppercase बनाता है — Title Case। User का नाम, city name, product title display करने के लिए perfect।

PHP 4+ Returns: string Title Case
string ucwords( string $string, string $separators = " \t\r\n\f\v" )
ParameterTypeज़रूरी?Description
$stringstringRequiredजिस string को title case करना है
$separatorsstringOptionalWord separators। Default: space, tab, newline
ReturnstringTitle case string
ucwords() — EXAMPLES
<?php
echo ucwords("rahul kumar"); // Rahul Kumar
echo ucwords("php tutorial in hindi"); // Php Tutorial In Hindi
echo ucwords("new delhi"); // New Delhi
echo ucwords("RAHUL KUMAR"); // RAHUL KUMAR (पहले से upper है)

// ध्यान दो — ucwords UPPERCASE को नहीं बदलता
// पहले strtolower करो, फिर ucwords
$naam = "RAHUL KUMAR";
echo ucwords(strtolower($naam)); // Rahul Kumar ✅
?>
ucwords() — REAL WORLD EXAMPLES
<?php
// 1. User का नाम proper case में display करना
$userNaam = " rahul kumar sharma ";
$display = ucwords(strtolower(trim($userNaam)));
echo $display; // Rahul Kumar Sharma

// 2. Blog post title
$title = "how to learn php in 30 days";
echo ucwords($title); // How To Learn Php In 30 Days

// 3. Custom separator — hyphen से separated words
$slug = "php-tutorial-hindi";
echo ucwords($slug, "-"); // Php-Tutorial-Hindi

// 4. ucfirst vs ucwords
echo ucfirst("rahul kumar"); // Rahul kumar (सिर्फ पहला)
echo ucwords("rahul kumar"); // Rahul Kumar (हर word)
?>
ucfirst() vs ucwords(): ucfirst() सिर्फ पहले character को capital बनाता है। ucwords() हर word के पहले character को। Name display के लिए ucwords, sentence start के लिए ucfirst use करो।

6
Comparison — पाँचों Functions एक नज़र में
FunctionInputOutputReturnsUse Case
strlen()"Hello World"11intPassword/input length check
strtoupper()"hello php""HELLO PHP"stringHeading display, comparison
strtolower()"HELLO PHP""hello php"stringEmail normalize, URL slug
trim()" hello ""hello"stringForm input clean करना
ucwords()"rahul kumar""Rahul Kumar"stringName/title display करना

7
Combined Example — User Registration Form
Real world में ये सभी functions एक साथ use होते हैं। यह देखो कि एक Registration form में सब कैसे काम करते हैं:
COMBINED — User Registration Processing
<?php
// User ने form submit किया — raw input
$rawNaam = " RAHUL KUMAR sharma ";
$rawEmail = " Rahul@Gmail.COM ";
$rawPassword = " mypass123 ";
$rawCity = " new delhi ";

// Step 1: trim() — extra spaces हटाओ
$naam = trim($rawNaam); // "RAHUL KUMAR sharma"
$email = trim($rawEmail); // "Rahul@Gmail.COM"
$password = trim($rawPassword); // "mypass123"
$city = trim($rawCity); // "new delhi"

// Step 2: Validate करो
$errors = [];
if (strlen($naam) < 3) {
  $errors[] = "नाम कम से कम 3 characters का होना चाहिए";
}
if (strlen($password) < 8) {
  $errors[] = "Password कम से कम 8 characters का होना चाहिए";
}

// Step 3: Normalize करो
$naam = ucwords(strtolower($naam)); // "Rahul Kumar Sharma"
$email = strtolower($email); // "rahul@gmail.com"
$city = ucwords($city); // "New Delhi"

// Step 4: Display करो
if (empty($errors)) {
  echo "✅ Welcome, " . $naam . "!";
  echo "Email: " . $email;
  echo "City: " . $city;
} else {
  foreach ($errors as $error) {
    echo "❌ " . $error . "<br>";
  }
}
?>
हर form input को process करने का सही order: trim() → strlen() validate → strtolower()/ucwords() normalize → database में store

निष्कर्ष (Conclusion)

यह पाँच functions PHP का daily toolkit हैं। कोई भी web application बनाओ — user registration, login, profile update — इन सबकी ज़रूरत पड़ेगी।

strlen() — length check, validation, preview truncate।

strtoupper() — headings, case-insensitive comparison।

strtolower() — email normalize, URL slug, file extension।

trim() — हर form input पर use करो — कभी skip मत करो।

ucwords() — names, titles display करना। पहले strtolower() करो।

🚀 अगला Sub-Chapter: Chapter 7.2 में strpos, str_contains, str_replace, substr, explode — String Search और Manipulation functions सीखें।