str_ireplace(): case in-sensitive String Replace

$search = "php";
$replace="JavaScript";
$final_string = str_ireplace($search, $replace, "Sites uses PHP along with HTML");
echo $final_string;
Output is here
Sites uses JavaScript along with HTML
Syntax
str_ireplace(search,replace,main_string,count);
search:
Required: String to be searched for matching, can be an array
replace:
Required: String to be replaced if found using search string, can be an array
main_string:
Required : String on which search and replace to be applied, can be an array
count:
Optional: Stores the number of replacements performed.


Output is a string or array after performing search and replace over the input string

The main difference between str_replace and str_ireplace is str_replace is a case sensitive string replacement and str_ireplace is a case in-sensitive string search and replacement.

str_ireplace() function in PHP when applied to a main string, it searches for occurrence of a string and replace them by another string.

Search Array & replace string

This function can take an array as search string and each element of the array if present is replaced by another string. We can try with an example. If you are searching for a simple example then you can see str_replace function. Here is a sample code which takes an array of search words and replaces them by another word within a string.

$search = array("good", "php", "html", "ASP");
$final_string = str_replace($search, "*", "All good sites uses PHP along with HTML or they use ASP and html");
echo $final_string;
Output is here
All * sites uses PHP along with HTML or they use * and *

Search array & replace array

As we have seen here we can use an array as search terms and all the elements of the array can be replaced by a replace string. We can also use another array as replacement array and each element is replace by matching element from replacement array, if not element is found then blank string is replaced. Here is the script code for that
$search = array("server", "php", "JSP", "asp");
$replace=array("client","HTML","JavaScript","CSS");
$final_string = str_ireplace($search, $replace, " Server side scripts are jsp, PHP and ASP ");
echo $final_string;
Output is here
client side scripts are JavaScript, HTML and CSS

Using optional parameter count to know the number of replacements

Above script can be modified to add optional parameter $count. This variable will store the number of search and replacement carried out.
$search = array("server", "php", "JSP", "asp");
$replace=array("client","HTML","JavaScript","CSS");
$final_string = str_ireplace($search, $replace, " Server side scripts are jsp, PHP and ASP ",$count);
echo $final_string;
echo "<br>Number of replacement= $count ";
Output is here
client side scripts are JavaScript, HTML and CSS 
Number of replacement= 4

Generate a clean prompt with str_ireplace() to use in AI tools

Pick one random keyword from a list and inject it into a reusable paragraph to build a posting prompt to AI Models. Case-insensitive replacement keeps the template simple and robust.

More on Gemini AI Models and passing prompts
<?php
/**
 * Practical use of str_ireplace():
 * Pick a random keyword from an array, inject into a prompt paragraph,
 * and (optionally) call your AI API to generate a social media post.
 */

// 1) Your keyword pool (expand as you like)
$keywords = [
    'Tkinter Treeview insert',
    'tkcalendar DateEntry',
    'PHP array unpacking',
    'MySQL BETWEEN query',
    'Regex with preg_match',
    'Python file handling',
];

// 2) Prompt paragraph with a case-insensitive placeholder {KEYWORD}
$promptTemplate = <<<TXT
Create a ~1500-character Google My Business post on: {KEYWORD}.

Audience: beginners/intermediate devs.
Focus: Explain ONE core outcome, include 1 tiny code line (no long blocks), and end BEFORE 1500 chars.
Tone: clear, helpful, no hype. Avoid keyword stuffing.
TXT;

// 3) Pick ONE keyword at random
if (empty($keywords)) {
    die("No keywords available.\n");
}
$randomKey = array_rand($keywords);
$selected  = $keywords[$randomKey];

// 4) Case-insensitive replacement (all occurrences)
//    {KEYWORD}, {keyword}, {KeyWord} … all replaced
$replacedCount = 0;
$finalPrompt = str_ireplace('{KEYWORD}', $selected, $promptTemplate, $replacedCount);

// 5) Show the ready-to-send prompt (you can stop here if you just copy/paste)
header('Content-Type: text/plain; charset=UTF-8');
echo "Selected keyword: {$selected}\n";
echo "Occurrences replaced: {$replacedCount}\n\n";
echo "---- PROMPT TO SEND ----\n";
echo $finalPrompt . "\n";
?>
Here is one sample Output
Create a ~1500-character Google My Business post on: tkcalendar DateEntry.

Audience: beginners/intermediate devs.
Focus: Explain ONE core outcome, include 1 tiny code line (no long blocks), and end BEFORE 1500 chars.
Tone: clear, helpful, no hype. Avoid keyword stuffing.

What Happens After the Prompt is Ready?

In the second part of this process, the dynamically generated prompt is passed to Google's Gemini AI model using a secure API call. The model processes the prompt and returns a polished, AI-generated social media post.

This demonstrates a full workflow—from preparing input using str_ireplace() to retrieving intelligent output from an AI system.

View Complete AI Social Media Post Example »


String Functions Case sensitive string replace
Subhendu Mohapatra — author at plus2net
Subhendu Mohapatra

Author

🎥 Join me live on YouTube

Passionate about coding and teaching, I publish practical tutorials on PHP, Python, JavaScript, SQL, and web development. My goal is to make learning simple, engaging, and project‑oriented with real examples and source code.



Subscribe to our YouTube Channel here



plus2net.com







John Komla

19-09-2009

This tutorial is 1000 times helpful to me. Thanks too much. But is it possible to show us how you built this form we are posting comments from using Ajax ? just like it is working now ? I mean insert records to database without page refresh using Ajax POST method. Thank you, John
smo

20-09-2009

Just read comment posting tutorial. But the Ajax based script is not yet available to download. You can download the basic one.




PHP video Tutorials
We use cookies to improve your browsing experience. . Learn more
HTML MySQL PHP JavaScript ASP Photoshop Articles Contact us
©2000-2025   plus2net.com   All rights reserved worldwide Privacy Policy Disclaimer