Welcome Guest, Not a member yet? Register   Sign In
Search text & highlight words in a range
#11

[eluser]sophistry[/eluser]
@frog

why did you copy and paste code from the CI helper functions? IMO, that is not a good idea.

i suggest re-writing your code so that it uses the helper functions as is.

1) if the helper functions are improved or a bug discovered, you benefit from the fix.
2) common understanding among devs as to what the function does.
3) documentation of the function.
4) it's just better to reuse working code rather than copy and paste it...
5) DRY (as in, don't repeat yourself - or CI code in this case)
6) etc...

in pseudo code (as in, not tested):
Code:
$this->load->helper('text');
$chopped_down_string = stristr($string, $phrase);
$limited_string = word_limiter($chopped_down_string);
$highlighted_string = word_highlight($limited_string);

if you need to generalize this code into a named function, then put this code in a library. use get_instance() to get an instance of CI so you can load the helper inside the library.

the future will smile on you.

incredible regex tutorial site:
http://www.regular-expressions.info/
#12

[eluser]adamfairholm[/eluser]
sophistry - good call, you are right. I would never do that with a library, so I shouldn't with a helper.

I've added this to a library that I've created that has some search algorithms. Here is the function:

Code:
function highlight_body($str, $term, $limit = 100, $tag_open, $tag_close, $end_char = '…', $start_char = '…')
    {
        $CI =& get_instance();

        $CI->load->helper('text');
            
          $sub_string = stristr($str, $term);
          
        if(!$sub_string)
        {
              $sub_string = $str;
              $start_char = '';
        }
        
        $sub_string = word_limiter($sub_string, $limit);
        
        return $start_char .= highlight_phrase($sub_string, $term, $tag_open, $tag_close);
    }




Theme © iAndrew 2016 - Forum software by © MyBB