← Back to PHP Course | Chapter 6: Strings | Lesson 3 of 8

PHP String Functions

String functions एक text toolbox में tools जैसे हैं: कुछ letters को बड़ा या छोटा करते हैं, दूसरे length measure करते हैं या spaces trim करते हैं। वे आपको words बदलने और inspect करने देते हैं बिना उन्हें दोबारा type किए।
Syntax
php
strtolower($string);
strtoupper($string);
trim($string);      // ltrim() / rtrim() for one side
strrev($string);
strlen($string);

String Case बदलना

strtolower() और strtoupper() एक ही call में string का letter case normalize करते हैं, जो उन comparisons के लिए ज़रूरी है जहाँ Yes, yes, और YES सबको same user input माना जाना चाहिए, तीन अलग strings नहीं।

उदाहरण: Changing String Case

php
<?php
// Declare `$input`, set to "YES"
$input = "YES";
// Print `strtolower($input) . "\n"` to the output
echo strtolower($input) . "\n";
// Print `strtoupper("no")` to the output
echo strtoupper("no");
?>

Whitespace Trim करना

trim() किसी string के दोनों सिरों से whitespace हटाता है; ltrim() और rtrim() उस removal को क्रमशः सिर्फ left या right side तक restrict करते हैं।

Store या compare करने से पहले user-submitted text को trim करना start या end पर किसी invisible stray space की वजह से होने वाले bugs से बचाता है।

उदाहरण: Trimming Whitespace

php
<?php
// Declare `$text`, set to "  hello  "
$text = "  hello  ";
// Print "[" . trim($text) . "]\n" to the output
echo "[" . trim($text) . "]\n";
// Print "[" . ltrim($text) . "]\n" to the output
echo "[" . ltrim($text) . "]\n";
// Print "[" . rtrim($text) . "]" to the output
echo "[" . rtrim($text) . "]";
?>

Strings को Reverse करना

strrev() किसी string के character order को पूरी तरह reverse कर देता है — hello olleh बन जाता है। Palindrome checks और simple text puzzles से आगे, यह ज़्यादातर एक demonstration function है बजाय everyday application code में इस्तेमाल होने वाली किसी चीज़ के।

उदाहरण: Reversing Strings

php
<?php
// Print `strrev("hello")` to the output
echo strrev("hello");
?>

Strings को Repeat और Pad करना

str_repeat() किसी दिए गए piece को एक set संख्या में बार दोहराकर एक string बनाता है, और str_pad() किसी string को एक चुने गए fill character इस्तेमाल करके एक specific target length तक extend करता है — दोनों output के fixed-width columns format करते समय common हैं।

उदाहरण: Repeating and Padding Strings

php
<?php
// Print `str_repeat("ab", 3) . "\n"` to the output
echo str_repeat("ab", 3) . "\n";
// Print `str_pad("5", 3, "0", STR_PAD_LEFT)` to the output
echo str_pad("5", 3, "0", STR_PAD_LEFT);
?>

Explode और Implode

explode() किसी string को टुकड़ों के एक array में split करता है जहाँ भी एक चुना गया delimiter दिखता है — comma पर 'a,b,c' को [a, b, c] में बदलते हुए।

implode() exactly उल्टा करता है, हर एक के बीच एक चुने गए separator के साथ किसी array के elements को वापस एक single string में जोड़ते हुए।

उदाहरण: Explode and Implode

php
<?php
// Declare `$parts`, set to `explode(",", "a,b,c")`
$parts = explode(",", "a,b,c");
// Print a human-readable dump of `$parts`
print_r($parts);
// Print `implode("-", $parts)` to the output
echo implode("-", $parts);
?>
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. return value को ignore करना, जैसे trim($s);, जबकि string functions एक नई string return करती हैं और original नहीं बदलतीं।
  2. non-English text पर strtolower() इस्तेमाल करना, जो multi-byte characters handle नहीं करता; mb_strtolower() इस्तेमाल करें।
  3. multi-byte text पर strrev() इस्तेमाल करना, जो bytes reverse करता है और characters corrupt कर देता है।

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.