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

PHP String Search और Replace

Strings पर search और replace किसी document में Find and Replace tool जैसा है। PHP आपको बता सकता है कि एक word किसी sentence के अंदर कहाँ बैठता है, या इसे किसी दूसरे word से swap कर सकता है।
Syntax
php
$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
<?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";
}
?>

Basic String Replacement

str_contains() (PHP 8+) सीधे इस सवाल का जवाब देता है कि क्या कोई substring किसी string के अंदर कहीं भी exist करता है, एक clean boolean की तरह, strpos() को false के against check करने वाले पुराने, ज़्यादा error-prone pattern की जगह लेते हुए।

उदाहरण: Basic String Replacement

php
<?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"));
?>

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
<?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);
?>

Case-Insensitively Search करना

substr() किसी दिए गए index से शुरू होकर एक optional दी गई length तक किसी string का एक हिस्सा extract करता है, जो यह है कि आप कैसे, मान लीजिए, किसी blog post के पहले 100 characters preview excerpt की तरह इस्तेमाल करने के लिए निकालेंगे।

उदाहरण: Searching Case-Insensitively

php
<?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);
?>

Index से हिस्से Replace करना

str_ireplace() और stripos() str_replace() और strpos() के case-insensitive counterparts हैं, तब उपयोगी जब आपको text ढूँढना या replace करना हो चाहे user ने इसे uppercase, lowercase, या mixed case में type किया हो।

उदाहरण: Replacing Portions by Index

php
<?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"));
?>
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. strpos($s, a) को if (strpos(...)) से test करना, जो false है जब match position 0 पर हो; इसके बजाय !== false से compare करें।
  2. str_replace इस्तेमाल करना और उम्मीद करना कि यह original variable बदल देगा, जबकि यह एक नई string return करता है।
  3. 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 करते हैं।

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.