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।
5Functions इस chapter में
stringसभी string return करते हैं
dailyहर PHP project में use
PHP 4+सभी functions available
📋 इस Article में क्या-क्या है
- strlen() — String Length
- strtoupper() — UPPERCASE
- strtolower() — lowercase
- trim() — Whitespace हटाना
- ucwords() — Title Case
- Comparison Table
- Combined Real World Example
1
strlen() — String की Length
int strlen( string $string )
| Parameter | Type | ज़रूरी? | Description |
|---|---|---|---|
| $string | string | Required | जिस string की length निकालनी है |
| Return | int | — | Characters की 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 करना
string strtoupper( string $string )
strtoupper() — EXAMPLES
<?phpecho 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 करना
string strtolower( string $string )
strtolower() — EXAMPLES
<?phpecho 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 करो
$email = strtolower(
trim($_POST['email'])
);
// Database में store करो
❌ बिना normalize किए store मत करो
// "Rahul@Gmail.COM" store होगा
// Login पर "rahul@gmail.com"
// Match नहीं होगा — Bug!
$email = $_POST['email'];
// Login पर "rahul@gmail.com"
// Match नहीं होगा — Bug!
$email = $_POST['email'];
4
trim() — Extra Spaces हटाना
string trim( string $string, string $characters = " \n\r\t\v\x00" )
| Parameter | Type | ज़रूरी? | Description |
|---|---|---|---|
| $string | string | Required | जिस string को trim करना है |
| $characters | string | Optional | कौन से characters हटाने हैं। Default: spaces, \n, \r, \t |
| Return | string | — | Trimmed 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
string ucwords( string $string, string $separators = " \t\r\n\f\v" )
| Parameter | Type | ज़रूरी? | Description |
|---|---|---|---|
| $string | string | Required | जिस string को title case करना है |
| $separators | string | Optional | Word separators। Default: space, tab, newline |
| Return | string | — | Title case string |
ucwords() — EXAMPLES
<?phpecho 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 एक नज़र में
| Function | Input | Output | Returns | Use Case |
|---|---|---|---|---|
| strlen() | "Hello World" | 11 | int | Password/input length check |
| strtoupper() | "hello php" | "HELLO PHP" | string | Heading display, comparison |
| strtolower() | "HELLO PHP" | "hello php" | string | Email normalize, URL slug |
| trim() | " hello " | "hello" | string | Form input clean करना |
| ucwords() | "rahul kumar" | "Rahul Kumar" | string | Name/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 सीखें।