PHP में, String (स्ट्रिंग) characters का एक sequence (क्रम) होता है। String का मतलब होता है “text” या “character का collection”।
अगर आप किसी text को स्टोर करना चाहते हैं, तो उसे PHP में String के रूप में represent किया जाता है।
String कैसे बनाते हैं?
PHP में String बनाना बहुत ही आसान है। हम single quotes (‘ ‘) या double quotes (” “) का इस्तेमाल करके String बना सकते हैं।
इसका उदाहरण:-
<?php
$name1 = 'Hello, World!'; // Single quotes का इस्तेमाल
$name2 = "Hello, PHP!"; // Double quotes का इस्तेमाल
echo $name1;
echo "<br>"; // Line break के लिए
echo $name2;
?>
इसका आउटपुट:–
Hello, World!
Hello, PHP!
Single Quotes और Double Quotes में अंतर:-
- Single Quotes (‘ ‘): इसमें हम सीधे text को स्टोर करते हैं। अगर आप variable की value दिखाना चाहते हैं, तो यह संभव नहीं है।
- Double Quotes (” “): इसमें आप variable का value सीधे print कर सकते हैं।
String को Access करना
PHP में String एक array की तरह काम करता है। आप String के characters को उनके index के द्वारा access कर सकते हैं। Indexing 0 से शुरू होती है।
उदाहरण:-
<?php
$text = "PHP";
echo $text[0]; // Output: P
echo $text[1]; // Output: H
echo $text[2]; // Output: P
?>
Loop से Access करना
अगर आप String के सभी characters को access करना चाहते हैं, तो आप loop का उपयोग कर सकते हैं।
<?php
$text = "Hello";
for ($i = 0; $i < strlen($text); $i++) {
echo $text[$i] . "\n";
}
?>
String Functions in PHP in Hindi
PHP में String के साथ काम करने के लिए बहुत सारे built-in functions हैं। ये functions हमें String को manipulate करने में मदद करते हैं। नीचे कुछ महत्वपूर्ण functions दिए गए हैं:-
1. strlen()
यह function किसी String की लंबाई (length) return करता है।
Example:
<?php
$text = "Hello, PHP!";
echo strlen($text); // Output: 12
?>
2. str_word_count()
यह function एक String में words की संख्या को return करता है।
Example:
<?php
$text = "Hello, how are you?";
echo str_word_count($text); // Output: 4
?>
3. strrev()
यह फंक्शन String को reverse (उल्टा) करता है।
Example:
<?php
$text = "Hello";
echo strrev($text); // Output: olleH
?>
4. strpos()
यह फंक्शन किसी substring की पहली occurrence का position (index) को return करता है। यह case-sensitive होता है।
Example:
<?php
$text = "I love PHP programming.";
$position = strpos($text, "PHP");
echo $position; // Output: 7
?>
5. strrpos()
यह फंक्शन किसी substring की आखिरी occurrence का position (index) को return करता है। यह भी case-sensitive होता है।
Example:
<?php
$text = "I love PHP, PHP is great!";
$position = strrpos($text, "PHP");
echo $position; // Output: 14
?>
6. substr()
यह फंक्शन किसी String के हिस्से (substring) को return करता है।
Example:
<?php
$text = "Hello, PHP!";
echo substr($text, 7, 3); // Output: PHP
?>
7. str_replace()
यह फंक्शन किसी substring को replace करता है। यह case-sensitive होता है।
Example:
<?php
$text = "I love PHP!";
$newText = str_replace("PHP", "Python", $text);
echo $newText; // Output: I love Python!
?>
8. str_ireplace()
यह फंक्शन किसी substring को case-insensitive तरीके से replace करता है।
Example:
<?php
$text = "I love php!";
$newText = str_ireplace("php", "Python", $text);
echo $newText; // Output: I love Python!
?>
9. trim()
यह फंक्शन String के शुरुआत और अंत से white spaces और दूसरे predefined characters को remove करता है।
Example:
<?php
$text = " Hello, PHP! ";
echo trim($text); // Output: Hello, PHP!
?>
10. ltrim() और rtrim()
ये String के शुरुआत (ltrim) और अंत (rtrim) से spaces या characters को remove करते हैं।
Example:
<?php
$text = " Hello, PHP! ";
echo ltrim($text); // Output: Hello, PHP!
echo rtrim($text); // Output: Hello, PHP!
?>
11. ucwords()
यह फंक्शन String के प्रत्येक word के पहले letter को uppercase करता है।
Example:
<?php
$text = "hello world";
echo ucwords($text); // Output: Hello World
?>
12. ucfirst()
यह फंक्शन String के पहले letter को uppercase करता है।
Example:
<?php
$text = "hello world";
echo ucfirst($text); // Output: Hello world
?>
13. str_split()
यह फंक्शन String को array में split करता है, जहां हर character एक element होता है।
Example:
<?php
$text = "Hello";
$array = str_split($text);
print_r($array);
// Output: Array ( [0] => H [1] => e [2] => l [3] => l [4] => o )
?>
14. str_pad()
यह फंक्शन String को एक विशेष length तक pad करता है।
Example:
<?php
$text = "PHP";
echo str_pad($text, 10, "-"); // Output: PHP-------
?>
15. explode()
यह function String को delimiter के द्वारा array में split करता है।
Example:
<?php
$text = "apple,banana,orange";
$array = explode(",", $text);
print_r($array);
// Output: Array ( [0] => apple [1] => banana [2] => orange )
?>
इसे पढ़ें:-
निवेदन:- अगर आपको यह पोस्ट उपयोगी लगी हो तो इसे अपने दोस्तों के साथ अवश्य share कीजिए। और आपके जो भी questions हैं उन्हें नीचे comment करके बताइये।