PHP String Functions
In this page:
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
// 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");
?>
Login to try C/C++/Java/PHP code in the editor
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
// 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) . "]";
?>
Login to try C/C++/Java/PHP code in the editor
Strings को Reverse करना
strrev() किसी string के character order को पूरी तरह reverse कर देता है — hello olleh बन जाता है। Palindrome checks और simple text puzzles से आगे, यह ज़्यादातर एक demonstration function है बजाय everyday application code में इस्तेमाल होने वाली किसी चीज़ के।
उदाहरण: Reversing Strings
<?php
// Print `strrev("hello")` to the output
echo strrev("hello");
?>
Login to try C/C++/Java/PHP code in the editor
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
// 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);
?>
Login to try C/C++/Java/PHP code in the editor
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
// 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);
?>
Login to try C/C++/Java/PHP code in the editor
- return value को ignore करना, जैसे
trim($s);, जबकि string functions एक नई string return करती हैं और original नहीं बदलतीं। - non-English text पर
strtolower()इस्तेमाल करना, जो multi-byte characters handle नहीं करता;mb_strtolower()इस्तेमाल करें। - multi-byte text पर
strrev()इस्तेमाल करना, जो bytes reverse करता है और characters corrupt कर देता है।
Chapter Quiz — Complete all 8 topics to unlock
0/8 topics done
Complete these topics first: