Software Technology Tips

PHP has many predefined functions. Whenever you face a problem and search for a solution there is 90% probablity that you can find a php predefined function to solve your problem. While most functions are supported by PHP 4 there are a few new ones have been introduced by PHP5. Often these functions look identical but they are not, lets take a look at the following functions:
 
str_ireplace and  str_replace  both functions take 3 parameters.
 
Parameter1 : The input string which you want to search
Parameter2 : The string which you want to over write on the search string
Parameter3 : The original String
 
What is the difference between str_ireplace and  str_replace
 The str_replacefunction is a case-sensitive which means that it replaces the string that exactly matches the string exactly. it means that if you call the function to replace the string "MFS" with "abc", the function would not replace strings which consist of  "mFS","mfs","MfS" because it wouldn't consider them a complete match.  
However, the str_ireplace function php is not sensitive-rule and will treat "MFS","mfs","MfS" ..... all combination as a single match. Please not that this fuction is available in PHP 5 only.
 
Sample : Suppose you have a set of results and there is a search features you want to search all records where "mfs" is present and you want to highlight the string "mfs" with a different color like red.
 
Please see the code below.
<?php
/***********************************************
suppose your string $str_result hold the query string from DB adn it is fetching the name and email id
of users and you want to search from a text fields named as search by entering mfs to search all users
having email id with mfs string
**********************************************/
//storing the search string
$searchstring = $_REQUEST['search'];
?>
<table>
               <tr>
                     <td><b>Name</b></td>
                     <td><b>Email</b></td>
               </tr>
                <?php
                while($rst_results = mysql_fetch_array($str_result ))
                {?>
                        <tr>
                                     <td>
                                         <?php echo $rst_results['name'];?>
                                     </td>
                                     <td>
                                          <?php
                                          echo str_ireplace($searchstring,"<font color=red>".$searchstring."</font>",$rst_results['email']);
                                          ?>
                                     </td>
                         < /tr>
                <?php 
                 }?>
</table>
 
 
<?php
/********************************************
echo str_ireplace($searchstring,"<font color=red>".$searchstring."</font>",$rst_results['email']);
here i am replacing the search string with the redcolor string.
********************************************/
?>


Related Tags:

.PHP

Author: Abinash Grahacharya

Related Pages

  • PHP Development India

PHP

Let us Connect!

iso 9001 QA25 Red Herring STPI D&B Fastest Growing SME 2013 Award zinnov Nasscom

This site uses cookies. We respect your privacy.copyright (c) Mindfire Solutions 2007-2015. Login