includes/functions.php
changeset 581 5e8fd89c02ea
parent 575 9c1ab9c74662
child 582 a38876c0793c
equal deleted inserted replaced
580:41c45314ac27 581:5e8fd89c02ea
  2829       // Got it!
  2829       // Got it!
  2830       $upto = substr($haystack, 0, $i);
  2830       $upto = substr($haystack, 0, $i);
  2831       $from = substr($haystack, ( $i + $needle_len ));
  2831       $from = substr($haystack, ( $i + $needle_len ));
  2832       $new_haystack = "{$upto}{$thread}{$from}";
  2832       $new_haystack = "{$upto}{$thread}{$from}";
  2833       return $new_haystack;
  2833       return $new_haystack;
       
  2834     }
       
  2835   }
       
  2836   return $haystack;
       
  2837 }
       
  2838 
       
  2839 /**
       
  2840  * Replaces all given occurences of needle in haystack, case insensitively.
       
  2841  * @param string Needle
       
  2842  * @param string Thread
       
  2843  * @param string Haystack
       
  2844  * @return string
       
  2845  */
       
  2846 
       
  2847 function str_replace_i($needle, $thread, $haystack)
       
  2848 {
       
  2849   $needle_len = strlen($needle);
       
  2850   $haystack_len = strlen($haystack);
       
  2851   for ( $i = 0; $i < $haystack_len; $i++ )
       
  2852   {
       
  2853     $test = substr($haystack, $i, $needle_len);
       
  2854     if ( strtolower($test) == strtolower($needle) )
       
  2855     {
       
  2856       // Got it!
       
  2857       $upto = substr($haystack, 0, $i);
       
  2858       $from = substr($haystack, ( $i + $needle_len ));
       
  2859       $haystack = "{$upto}{$thread}{$from}";
       
  2860       $haystack_len = strlen($haystack);
       
  2861       $i = $i + strlen($thread);
       
  2862     }
       
  2863   }
       
  2864   return $haystack;
       
  2865 }
       
  2866 
       
  2867 /**
       
  2868  * Highlights a term in a string.
       
  2869  * @param string Needle (term to highlight)
       
  2870  * @param string Haystack (search string)
       
  2871  * @param string Starting tag (<b>)
       
  2872  * @param string Ending tag (</b>)
       
  2873  * @return string
       
  2874  */
       
  2875 
       
  2876 function highlight_term($needle, $haystack, $start_tag = '<b>', $end_tag = '</b>')
       
  2877 {
       
  2878   $needle_len = strlen($needle);
       
  2879   $haystack_len = strlen($haystack);
       
  2880   for ( $i = 0; $i < $haystack_len; $i++ )
       
  2881   {
       
  2882     $test = substr($haystack, $i, $needle_len);
       
  2883     if ( strtolower($test) == strtolower($needle) )
       
  2884     {
       
  2885       // Got it!
       
  2886       $upto = substr($haystack, 0, $i);
       
  2887       $from = substr($haystack, ( $i + $needle_len ));
       
  2888       $haystack = "{$upto}{$start_tag}{$test}{$end_tag}{$from}";
       
  2889       $haystack_len = strlen($haystack);
       
  2890       $i = $i + strlen($needle) + strlen($start_tag) + strlen($end_tag);
  2834     }
  2891     }
  2835   }
  2892   }
  2836   return $haystack;
  2893   return $haystack;
  2837 }
  2894 }
  2838 
  2895