CodeIgniter Forums
Extract text from MEDIUM TEXT - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forum-20.html)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forum-23.html)
+--- Thread: Extract text from MEDIUM TEXT (/thread-10675.html)



Extract text from MEDIUM TEXT - El Forum - 08-08-2008

[eluser]abmcr[/eluser]
This is not a CI problem... but a suggestion
I have a MEDIUM TEXT from a database (for example
Code:
CodeIgniter is a powerful PHP framework with a very small footprint, built for PHP
coders who need a simple and elegant toolkit to create full-featured web applications.
If you're a developer who lives in the real world of shared hosting accounts and clients with deadlines,
and if you're tired of ponderously large and thoroughly undocumented frameworks
and i want to extract a small piece, for example 10 words before "deadlines" and 10 words after...


The function strpos of PHP is useful (and i use it if no idea), but i think a regexp is better.... Thank you


Extract text from MEDIUM TEXT - El Forum - 08-08-2008

[eluser]Michael Wales[/eluser]
PHP's str functions are a lot faster than RegEx.


Extract text from MEDIUM TEXT - El Forum - 08-08-2008

[eluser]drewbee[/eluser]
Hmm strange my reply didn't take.

Anyways, I will type it out again.

Personally... I would use explode to handle this.

1. Explode string to array (seperate by spaces);
2. Loop over array until array value ('deadlines') is found, obtain key.
3. (2nd loop) Unset all values from the array whos key < deadlineKey - 10 and key > deadlineKey + 10.
4. Implode array (seperate by spaces)

This will result in a string containing 10 words before and after the word deadline.

Please note you will have to do some checking to insure after doing the math that you are not trying to unset any values < 0 (first word) and any values greater then count(array) (last word), otherwise errors will be thrown!


Extract text from MEDIUM TEXT - El Forum - 08-08-2008

[eluser]abmcr[/eluser]
Code:
function becca($stringa_ricercata,$text,$parole=25,$end_char="...")
    {
        $CI =& get_instance();
        $CI->load->helper("text");
        $init=strripos(($text),($stringa_ricercata)); //posizione parola
        if ($init==0) return highlight_phrase(word_limiter($text,$parole), $stringa_ricercata, '<span style="color:#990000">', '</span>');//esce se รจ il primo in realta' word_limiter
        $words = explode(' ', preg_replace("/\s+/", ' ', preg_replace("/(\r\n|\r|\n)/", " ", strtolower($text))));
        $parola=array_keys($words, strtolower($stringa_ricercata));
        $posto_parola=$parola[0]; //numero parola
        $ini=(($posto_parola-$parole)<0?0:($posto_parola-$parole));
        $end=(($posto_parola+$parole)>count($words)?count($words):($posto_parola+$parole));
        if (count($words) <= $parole)
        {
            return highlight_phrase(trim($text), $stringa_ricercata, '<span style="color:#990000">', '</span>');
        }

        $str = '';
        for ($i = $ini; $i < $end; $i++)
        {
            $str .= $words[$i].' ';
        }
        if ($ini>0) $str=$end_char.$str;
        return highlight_phrase(trim($str).$end_char, $stringa_ricercata, '<span style="color:#990000">', '</span>');
    }

i have made a function in a librarie... that's because i use $CI->

Thank you


Extract text from MEDIUM TEXT - El Forum - 08-08-2008

[eluser]xwero[/eluser]
It seems a strange thing to do but who am i to judge Smile

Code:
function get_str_by_middle_word($str,$word,$word_limit = 10)
{
    $words = str_word_count($str);
    if(($key = array_search($word,$words) === FALSE)
    {
        return '';
    }

    $start = ($key < $word_limit)?0:$key-$word_limit;
    
    if($key+$word_limit > count($words)-1)
    {
        return implode(' ',array_slice($words,$start));
    }
    else
    {
        return implode(' ',array_slice($words,$start,$key+$word_limit));
    }
}
The code is untested.