PHP Strings क्या है?
Complete Guide in Hindi
PHP Strings की पूरी जानकारी हिंदी में — String declare करना, Quotes, Escape sequences, 20+ String Functions, Heredoc, Nowdoc। Real examples के साथ।
📋 इस Article में क्या-क्या है (Table of Contents)
- String क्या है?
- Single vs Double Quotes
- Escape Sequences
- Heredoc & Nowdoc
- String Length & Case Functions
- String Search Functions
- String Replace Functions
- String Cut Functions
- String Split & Join
- Quick Reference + FAQ
String characters का एक sequence है — letters, numbers, spaces, symbols कुछ भी। PHP में string को quotes में लिखते हैं। String PHP का सबसे ज़्यादा use होने वाला data type है।
$naam = "Rahul Kumar"; // double quotes
$city = 'Delhi'; // single quotes
$email = "rahul@email.com";
$number = "9876543210"; // number भी string हो सकता है
$empty = ""; // empty string
echo $naam; // Rahul Kumar
echo strlen($naam); // 11 (characters count)
?>
✅ Double Quotes " " — Variables expand होते हैं
// Variable directly expand होता है
echo "Hello $naam!";
// Output: Hello Rahul!
// Complex — curly braces use करो
echo "Name: {$naam}s";
// Output: Name: Rahuls
⚠️ Single Quotes ' ' — Variables expand नहीं
// Variable literally print होता है
echo 'Hello $naam!';
// Output: Hello $naam!
// Escape sequences भी नहीं
echo '\n';
// Output: \n (literally)
$product = "Laptop";
$price = 45000;
// Double quotes — variable interpolation
echo "Product: $product, Price: ₹$price";
// Product: Laptop, Price: ₹45000
// Single quotes — faster (no variable parsing)
echo 'यह static text है, variables नहीं बदलते';
// Quote के अंदर quote — दूसरी type use करो
echo "He said 'Hello'"; // double में single
echo 'She said "Hi"'; // single में double
?>
| Sequence | मतलब | Example |
|---|---|---|
| \n | Newline (नई लाइन) | "Line1\nLine2" |
| \t | Tab (खाली जगह) | "Name:\tRahul" |
| \\ | Backslash (\) | "C:\\Users\\Rahul" |
| \" | Double quote | "He said \"Hi\"" |
| \' | Single quote | 'It\'s a book' |
| \$ | Dollar sign | "Price: \$500" |
| \r | Carriage return | Windows line ending |
| \0 | Null character | Binary data में |
// \n — newline (HTML में br tag ज़रूरी होगा)
echo "पहली लाइन\nदूसरी लाइन\nतीसरी लाइन";
// \t — tab spacing
echo "नाम:\tRahul\nशहर:\tDelhi";
// \" — double quote inside string
echo "उसने कहा \"नमस्ते!\"";
// Output: उसने कहा "नमस्ते!"
// \$ — literal dollar sign
echo "Price: \$500";
// Output: Price: $500
// \\ — backslash in Windows paths
echo "Path: C:\\Users\\Rahul\\Documents";
?>
Heredoc
Double quotes जैसा — variables expand होते हैं। Long HTML templates के लिए best।
Nowdoc
Single quotes जैसा — variables expand नहीं होते। Static templates के लिए।
$naam = "Rahul";
$city = "Delhi";
$score = 95;
// Heredoc — variables expand होते हैं
$html = <<<EOT
<div class="card">
<h2>$naam</h2>
<p>शहर: $city</p>
<p>Score: $score%</p>
</div>
EOT;
echo $html;
// Nowdoc — variables expand नहीं होते
$template = <<<'EOT'
Hello $naam, यह literally print होगा।
EOT;
echo $template; // Hello $naam, यह literally print होगा।
?>
String Length
String में characters की count देता है।
strlen("Hello") → 5Uppercase बनाना
पूरी string को CAPITAL letters में।
strtoupper("hello") → HELLOLowercase बनाना
पूरी string को small letters में।
strtolower("HELLO") → helloFirst Letter Capital
पहले character को uppercase।
ucfirst("rahul") → RahulEvery Word Capital
हर word का पहला letter uppercase।
ucwords("rahul kumar") → Rahul KumarWhitespace हटाना
शुरू और अंत से spaces हटाता है।
trim(" hello ") → "hello"$naam = " rahul kumar "; // spaces के साथ
// Form input clean करना
$clean = trim($naam); // "rahul kumar"
$proper = ucwords($clean); // "Rahul Kumar"
echo $proper; // Rahul Kumar
// Password length check
$password = "mypass123";
if (strlen($password) < 8) {
echo "Password कम से कम 8 characters का होना चाहिए!";
}
// Email को lowercase बनाना
$email = "Rahul@Gmail.COM";
$email = strtolower(trim($email));
echo $email; // rahul@gmail.com
// ltrim और rtrim — सिर्फ एक side से
echo ltrim(" hello "); // "hello " (left trim)
echo rtrim(" hello "); // " hello" (right trim)
?>
Position ढूँढना
String में substring की पहली position। नहीं मिले तो false।
strpos("Hello World", "World") → 6Last Position
Substring की last (आखिरी) position।
strrpos("abcabc", "c") → 5Contains Check (PHP 8)
String में substring है या नहीं — true/false।
str_contains("Hello", "ell") → trueStarts With (PHP 8)
String किसी से शुरू होती है?
str_starts_with("Hello", "He") → trueEnds With (PHP 8)
String किसी से खत्म होती है?
str_ends_with("hello.php", ".php") → trueCount Occurrences
String में substring कितनी बार आई।
substr_count("hello", "l") → 2$email = "rahul@gmail.com";
$url = "https://hindinotespoint.com";
// strpos — position ढूँढना
$pos = strpos($email, "@");
echo $pos; // 5
// strpos — false check (=== use करो!)
if (strpos($email, "@") !== false) {
echo "Valid email format";
}
// PHP 8 — str_contains (much cleaner!)
if (str_contains($email, "@")) {
echo "@ है email में";
}
// str_starts_with — URL check
if (str_starts_with($url, "https")) {
echo "Secure URL ✓";
}
// str_ends_with — file extension check
$file = "photo.jpg";
if (str_ends_with($file, ".jpg") || str_ends_with($file, ".png")) {
echo "Valid image file";
}
?>
// Basic replace
$text = "Hello World";
echo str_replace("World", "PHP", $text);
// Hello PHP
// Case-insensitive replace
$msg = "I love JAVA and java is great";
echo str_ireplace("java", "PHP", $msg);
// I love PHP and PHP is great
// Array से multiple replace एक साथ
$search = ["bad", "worst", "ugly"];
$replace = ["good", "best", "beautiful"];
$review = "This is bad and ugly product";
echo str_replace($search, $replace, $review);
// This is good and beautiful product
// URL slug बनाना
$title = "PHP Strings Tutorial in Hindi";
$slug = strtolower(str_replace(" ", "-", $title));
echo $slug; // php-strings-tutorial-in-hindi
?>
// Phone number mask करना
$phone = "9876543210";
$masked = substr_replace($phone, "XXXXXX", 0, 6);
echo $masked; // XXXXXX3210
// nl2br — newlines को HTML br में convert
$msg = "Line 1\nLine 2\nLine 3";
echo nl2br($msg); // Line 1<br>Line 2<br>Line 3
?>
$str = "HindiNotesPoint";
// 0123456789...
// substr(string, start, length)
echo substr($str, 0, 5); // Hindi (0 से 5 chars)
echo substr($str, 5, 5); // Notes (5 से 5 chars)
echo substr($str, 10); // Point (10 से end तक)
echo substr($str, -5); // Point (आखिरी 5 chars)
// Blog preview — 100 chars + "..."
$content = "PHP एक बहुत powerful language है जिससे बड़ी-बड़ी websites बनती हैं।";
if (strlen($content) > 50) {
echo substr($content, 0, 50) . "...";
}
// Email से username निकालना
$email = "rahul@gmail.com";
$atPos = strpos($email, "@");
$username = substr($email, 0, $atPos);
echo $username; // rahul
?>
// CSV data process करना
$csv = "Rahul,Priya,Amit,Neha";
$names = explode(",", $csv);
print_r($names);
// Array ( [0] => Rahul [1] => Priya [2] => Amit [3] => Neha )
// Date split करना
$date = "2025-12-25";
$parts = explode("-", $date);
echo "Year: " . $parts[0]; // 2025
echo "Month: " . $parts[1]; // 12
echo "Day: " . $parts[2]; // 25
// URL path split
$path = "/php/strings/functions";
$segments = explode("/", trim($path, "/"));
print_r($segments); // [php, strings, functions]
?>
$tags = ["PHP", "Laravel", "MySQL", "HTML"];
// Array को comma-separated string बनाना
echo implode(", ", $tags);
// PHP, Laravel, MySQL, HTML
// SQL query में use
$ids = [1, 2, 3, 4, 5];
$query = "SELECT * FROM users WHERE id IN (" . implode(",", $ids) . ")";
echo $query; // SELECT * FROM users WHERE id IN (1,2,3,4,5)
// Path बनाना
$parts = ["home", "user", "documents"];
echo implode("/", $parts); // home/user/documents
?>
| Function | काम | Example → Result |
|---|---|---|
| strlen() | Length निकालना | strlen("Hello") → 5 |
| strtoupper() | UPPERCASE | strtoupper("hi") → HI |
| strtolower() | lowercase | strtolower("HI") → hi |
| ucfirst() | First letter capital | ucfirst("rahul") → Rahul |
| ucwords() | Every word capital | ucwords("rahul kumar") → Rahul Kumar |
| trim() | Whitespace हटाना | trim(" hi ") → "hi" |
| strpos() | Position ढूँढना | strpos("Hello", "l") → 2 |
| str_contains() | Contains check (PHP8) | str_contains("Hi", "H") → true |
| str_replace() | Replace करना | str_replace("a","b","cat") → cbt |
| substr() | हिस्सा निकालना | substr("Hello", 1, 3) → ell |
| explode() | String → Array | explode(",","a,b,c") → [a,b,c] |
| implode() | Array → String | implode("-",[a,b,c]) → a-b-c |
| str_repeat() | Repeat करना | str_repeat("ab", 3) → ababab |
| str_word_count() | Words count | str_word_count("Hi there") → 2 |
| str_pad() | Padding add करना | str_pad("5", 3, "0", STR_PAD_LEFT) → 005 |
| nl2br() | \n → <br> | HTML display के लिए |
| htmlspecialchars() | XSS से बचाना | User input को safe बनाना |
| sprintf() | Formatted string | sprintf("%.2f", 3.1) → 3.10 |
PHP Strings सबसे ज़्यादा use होने वाला data type है। User input, database data, HTML output — सब strings में होता है। 100+ string functions की ज़रूरत नहीं याद रखने की — 15-20 important functions daily use होते हैं।
Double quotes में variables expand होते हैं — single quotes में नहीं।
trim() + strtolower() — user input clean करने के लिए हमेशा use करो।
strpos() में === false use करो — position 0 को false मत मानो।
PHP 8 के str_contains/starts_with/ends_with बहुत cleaner हैं।
explode/implode — CSV data, URLs, tags के लिए daily use होते हैं।
htmlspecialchars() — user input print करते समय हमेशा use करो।