Current Location: Home> Latest Articles> How to Use strval and str_ireplace Functions for Case-Insensitive String Replacement?

How to Use strval and str_ireplace Functions for Case-Insensitive String Replacement?

gitbox 2025-09-12
<span><span><span class="hljs-meta">&lt;?php</span></span><span>
</span><span><span class="hljs-comment">// Example of unrelated preliminary code</span></span><span>
</span><span><span class="hljs-variable">$timestamp</span></span><span> = </span><span><span class="hljs-title function_ invoke__">time</span></span><span>();
</span><span><span class="hljs-keyword">echo</span></span><span> </span><span><span class="hljs-string">"Current timestamp: <span class="hljs-subst">$timestamp</span></span></span><span>";
</span><span><span class="hljs-meta">?&gt;</span></span><span>
<p><hr></p>
<p></span><?php<br>
// Main content starts</p>
<p>/**</p>
<ul>
<li>
<p>Title: How to Use strval and str_ireplace Functions for Case-Insensitive String Replacement?</p>
</li>
<li></li>
<li>
<p>In PHP development, string replacement is a common operation. Especially when dealing with user input or text content,</p>
</li>
<li>
<p>sometimes we need to perform replacements without considering letter case. PHP provides two very useful functions for this purpose:</p>
</li>
<li>
<p>strval and str_ireplace.</p>
</li>
<li></li>
<li>
<ol>
<li>
<p>strval Function</p>
</li>
</ol>
</li>
<li>
<p>strval() converts any data type into a string, which is very useful to ensure that replacement operations run smoothly.</p>
</li>
<li>
<p>Example:<br>
*/</p>
</li>
</ul>
<p>$number = 12345;<br>
$stringNumber = strval($number);  // Convert integer to string<br>
</span>echo "Converted string: $stringNumber\n";</p>
<p>/**</p>
<ul>
<li>
<p>2. str_ireplace Function</p>
</li>
<li>
<p>str_ireplace() is a case-insensitive string replacement function, similar to str_replace(),</p>
</li>
<li>
<p>but ignores letter case.</p>
</li>
<li></li>
<li>
<p>Function signature:</p>
</li>
<li>
<p>str_ireplace(mixed $search, mixed $replace, mixed $subject, int &$count = null): mixed</p>
</li>
<li></li>
<li>
<p>Parameters:</p>
</li>
<li>
<ul>
<li>
<p>$search: The string or array of strings to search for.</p>
</li>
</ul>
</li>
<li>
<ul>
<li>
<p>$replace: The string or array of strings to replace with.</p>
</li>
</ul>
</li>
<li>
<ul>
<li>
<p>$subject: The target string or array to perform replacements on.</p>
</li>
</ul>
</li>
<li>
<ul>
<li>
<p>$count: Optional parameter to receive the number of replacements.<br>
*/</p>
</li>
</ul>
</li>
</ul>
<p>/**</p>
<ul>
<li>
<p>Example: Case-insensitive string replacement<br>
*/<br>
$text = "Hello World! hello world!";<br>
$search = "HELLO";<br>
$replace = "Hi";</p>
</li>
</ul>
<p>// Perform replacement using str_ireplace<br>
$result = str_ireplace($search, $replace, $text);</p>
<p>echo "Replacement result: $result\n";<br>
// Output: "Hi World! Hi world!"</p>
<p>/**</p>
<ul>
<li>
<p>3. Combining strval and str_ireplace</p>
</li>
<li>
<p>Sometimes the value to be replaced may not be a string, such as numbers or booleans retrieved from a database or API.</p>
</li>
<li>
<p>In this case, first use strval to convert it to a string, then perform a case-insensitive replacement.<br>
*/</p>
</li>
</ul>
<p>$input = 1001;<br>
$searchValue = "1001";<br>
$replaceValue = "One Thousand One";</p>
<p>// Convert to string<br>
$inputStr = strval($input);</p>
<p>// Perform case-insensitive replacement<br>
$finalResult = str_ireplace($searchValue, $replaceValue, $inputStr);</p>
<p>echo "Final replacement result: $finalResult\n";<br>
// Output: "One Thousand One"</p>
<p>/**</p>
<ul data-is-only-node="" data-is-last-node="">
<li>
<p>Summary:</p>
</li>
<li>
<ul>
<li>
<p>strval ensures data is in string format, avoiding type errors.</p>
</li>
</ul>
</li>
<li>
<ul>
<li>
<p>str_ireplace performs case-insensitive string replacements, ideal for handling user input and text data.</p>
</li>
</ul>
</li>
<li data-is-last-node="">
<ul data-is-last-node="">
<li data-is-last-node="">
<p data-is-last-node="">Using both together allows safe and flexible handling of various types of data replacements.<br>
*/<br>
?><br>
</span>