How can one maintain the original capitalization when replacing a search term in a string using PHP?
When replacing a search term in a string using PHP, one can maintain the original capitalization by first storing the original capitalization of the search term, replacing all occurrences of the search term in the string with a lowercase version, and then using the stored original capitalization to replace the lowercase version with the correct capitalization.
$searchTerm = "SearchTerm";
$string = "This is a SearchTerm example with SearchTerm in it.";
$lowercaseSearchTerm = strtolower($searchTerm);
$string = str_ireplace($lowercaseSearchTerm, $searchTerm, $string);
echo $string;
Related Questions
- How can PHP be used to gather visitor information for a visitor exchange service without requiring registration?
- Are there any specific PHP functions or tools that can help detect bracket errors in code?
- How can the fopen() function in PHP be utilized to read and compare domain names from a text file for blocking purposes?