PHP String Search और Replace
In this page:
$position = strpos($haystack, $needle); // false if not found
str_contains($haystack, $needle); // PHP 8+
$result = str_replace($search, $replace, $subject);
Substring Position ढूँढना
strpos() किसी string के अंदर एक substring की पहली occurrence की numeric position return करता है, या न मिलने पर false — एक common pitfall == false के बजाय === false इस्तेमाल करना है, क्योंकि position 0 पर एक match loose comparison के तहत falsy है।
उदाहरण: Finding Substring Position
<?php
// Declare `$text`, set to "Hello World"
$text = "Hello World";
// Declare `$pos`, set to `strpos($text, "World")`
$pos = strpos($text, "World");
// Check whether `$pos !== false`
if ($pos !== false) {
// Print "Found at position $pos" to the output
echo "Found at position $pos";
}
?>
Login to try C/C++/Java/PHP code in the editor
Basic String Replacement
str_contains() (PHP 8+) सीधे इस सवाल का जवाब देता है कि क्या कोई substring किसी string के अंदर कहीं भी exist करता है, एक clean boolean की तरह, strpos() को false के against check करने वाले पुराने, ज़्यादा error-prone pattern की जगह लेते हुए।
उदाहरण: Basic String Replacement
<?php
// Check whether `!function_exists('str_contains')`
if (!function_exists('str_contains')) {
// Define the function `str_contains` taking `$haystack`, `$needle`
function str_contains($haystack, $needle) {
// Return `strpos($haystack, $needle) !== false` from this function
return strpos($haystack, $needle) !== false;
}
}
// Declare `$text`, set to "Hello World"
$text = "Hello World";
// Print a detailed dump (with types) of `str_contains($text, "World")`
var_dump(str_contains($text, "World"));
?>
Login to try C/C++/Java/PHP code in the editor
Case-Insensitive Replacement
str_replace() एक target substring की हर occurrence को एक replacement से swap कर देता है, और दोनों arguments के लिए arrays accept करता है ताकि आप एक ही call में कई find-and-replace operations perform कर सकें, जैसे एक साथ कई unwanted characters हटाना।
उदाहरण: Case-Insensitive Replacement
<?php
// Declare `$text`, set to "The Quick Brown Fox"
$text = "The Quick Brown Fox";
// Print `str_replace(["Quick", "Brown"], ["Slow", "Red"], $text)` to the output
echo str_replace(["Quick", "Brown"], ["Slow", "Red"], $text);
?>
Login to try C/C++/Java/PHP code in the editor
Case-Insensitively Search करना
substr() किसी दिए गए index से शुरू होकर एक optional दी गई length तक किसी string का एक हिस्सा extract करता है, जो यह है कि आप कैसे, मान लीजिए, किसी blog post के पहले 100 characters preview excerpt की तरह इस्तेमाल करने के लिए निकालेंगे।
उदाहरण: Searching Case-Insensitively
<?php
// Declare `$post`, set to "This is a long blog post about PHP arrays and functions."
$post = "This is a long blog post about PHP arrays and functions.";
// Print `substr($post, 0, 20)` to the output
echo substr($post, 0, 20);
?>
Login to try C/C++/Java/PHP code in the editor
Index से हिस्से Replace करना
str_ireplace() और stripos() str_replace() और strpos() के case-insensitive counterparts हैं, तब उपयोगी जब आपको text ढूँढना या replace करना हो चाहे user ने इसे uppercase, lowercase, या mixed case में type किया हो।
उदाहरण: Replacing Portions by Index
<?php
// Declare `$text`, set to "Hello WORLD"
$text = "Hello WORLD";
// Print `str_ireplace("world", "PHP", $text) . "\n"` to the output
echo str_ireplace("world", "PHP", $text) . "\n";
// Print a detailed dump (with types) of `stripos($text, "world")`
var_dump(stripos($text, "world"));
?>
Login to try C/C++/Java/PHP code in the editor
strpos($s, a)कोif (strpos(...))से test करना, जो false है जब match position 0 पर हो; इसके बजाय!== falseसे compare करें।str_replaceइस्तेमाल करना और उम्मीद करना कि यह original variable बदल देगा, जबकि यह एक नई string return करता है।str_replaceइस्तेमाल करना और उम्मीद करना कि यह case ignore करेगा, जबकि case-insensitive replacement के लिए आपकोstr_ireplaceचाहिए।
- Strings escape characters, string functions, और formatting support करती हैं।
- Heredoc और nowdoc लंबे text को handle करते हैं, और search और replace text content बदलते हैं।
- Regular expressions text में patterns match करते हैं।
Chapter Quiz — Complete all 8 topics to unlock
0/8 topics done
Complete these topics first: