Need some help with a search replace function I did.

Discussion in 'PHP' started by exodus, Jan 11, 2008.

  1. #1
      function search_match($desc,$searchstr)
      {
        $searchar = explode(" ", $searchstr);
        $total = count($searchar)-1;
        for($c=0; $c <= $total; $c++)
        {
          $desc = str_replace($searchar[$c],"<b>".$searchar[$c]."</b>", $desc); 
        }    
       
        return $desc;
      }
    Code (markup):

    Anyone know how to make this where it will replace it and be non case sensitive? I do a word search for ms swan, it will not bold MS, Ms, Swan, SWan, SWAn, SWAN, etc. It will only bold the ms swan one. :(
    ----------
    str_ireplace is only for php5 and I am working with php 4.3
     
    exodus, Jan 11, 2008 IP
  2. WeBuster

    WeBuster Member

    Messages:
    27
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    38
    #2
    Read about stripos() function.

    Then use strtolower() function to replace easily.
     
    WeBuster, Jan 11, 2008 IP
  3. exodus

    exodus Well-Known Member

    Messages:
    1,900
    Likes Received:
    35
    Best Answers:
    0
    Trophy Points:
    165
    #3
    strtolower will lower both of them. I want to leave the case alone in the original string the shown string, but just add bold around it.

    Thanks for the help. Still trying to figure it out.
     
    exodus, Jan 11, 2008 IP
  4. lfhost

    lfhost Peon

    Messages:
    232
    Likes Received:
    8
    Best Answers:
    0
    Trophy Points:
    0
    #4
    use

    str_ireplace() and not str_replace()
     
    lfhost, Jan 11, 2008 IP
  5. exodus

    exodus Well-Known Member

    Messages:
    1,900
    Likes Received:
    35
    Best Answers:
    0
    Trophy Points:
    165
    #5
    lfhost: don't have php 5. Even that would not be replacing it as it was, but as a constant.
     
    exodus, Jan 11, 2008 IP
  6. WeBuster

    WeBuster Member

    Messages:
    27
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    38
    #6
    I know it will lower both of them, But look at this:
    
    <?php
    $str = "rsgx gfsd MS SwAn kjh";
    $search = "ms swan";
    
    $searchLen = strlen($search);
    $where = stripos($str, $search);
    $theSame = substr($str, $where, $searchLen);
    
    $desc = str_replace($theSame,"<b>".$theSame."</b>", $str);
    ?>
    
    PHP:
    $str == $desc .
    It's the same string, but "ms swan" is bold.

    Emm...Here is the full function:
    
    <?php
    function search_match($str,$search)
    {
    	$search = strtolower($search);
    	$searchLen = strlen($search);
    
    	$where = stripos($str, $search);
    	$theSame = substr($str, $where, $searchLen);
    
    	$desc = str_replace($theSame,"<b>".$theSame."</b>", $str);
    
    	return $desc;
    }
    ?>
    
    PHP:
     
    WeBuster, Jan 12, 2008 IP
  7. lfhost

    lfhost Peon

    Messages:
    232
    Likes Received:
    8
    Best Answers:
    0
    Trophy Points:
    0
    #7
    lfhost, Jan 12, 2008 IP
  8. exodus

    exodus Well-Known Member

    Messages:
    1,900
    Likes Received:
    35
    Best Answers:
    0
    Trophy Points:
    165
    #8
    lfhost: hrm.. thanks! I think this might work..

    <?php
    $pattern = '(>[^<]*)('. quotemeta($_GET['search']) .')';
    $replacement = '\\1<span class="search">\\2</span>';
    $body = eregi_replace($pattern, $replacement, $body);
    ?>
    
    Code (markup):
    Will have to modify it a bit, but I get the jest of it.
     
    exodus, Jan 12, 2008 IP