str_ireplace()Replaces “php” with “JavaScript” (ignoring case) and prints the result.
$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:replace:main_string:count:
$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("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
$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
str_ireplace() to use in AI toolsPick 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.
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.
Author & Instructor at plus2net
I write and maintain practical tutorials on Python, PHP, SQL, JavaScript, HTML, jQuery, and web development at plus2net. The tutorials focus on clear explanations, working examples, and code that readers can test and adapt while learning.
| 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. | |