[PHP] - Zoekfunctie, 11 woorden

Pagina: 1
Acties:

Onderwerpen


Acties:
  • 0 Henk 'm!

Anoniem: 283927

Topicstarter
Ik heb een zoekfunctie waarbij ik de eerste 5 woorden voor de zoekterm wil hebben, de zoekterm zelf en dan nog eens de 5 woorden daarachter. Nu ben ik met het volgende gekomen

PHP:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
<?php
for ($i = 0; $i < $countSearchResults; $i++)
{
    //Build up of show partial results
    $found = array();
    //replace all crap of the array with space and break the string up in an array.
    $searchContent = removeNewLines($searchResults[$i]['content']);
    $searchContent = explode(" ", $searchContent);
    //Check if the searchterm matches the value.
    foreach($searchContent as $key => $value)
    {
        // optionaly use eregi for case insensitiveness
        if(eregi($searchTerm, $value))
        { 
            $found[$key] = '<strong>'.$value.'</strong>';
        }
    }
    //Build new array after clearing whitespaces and all other stuff
    $searchContent = array_filter($searchContent);
    //Check wich keys contain the searchterms.
    $arrayKey = array_keys($found);
    if(is_array($arrayKey) && count($arrayKey) > 0)
    {
        $countArrayKeys = count($arrayKey);
        for($j = 0; $j < $countArrayKeys; $j++)
        {
            $key = $arrayKey[$j];
            if ($j == ($countArrayKeys -1))
            {
                $prevKey = 0;
                if($j != 0)
                {
                    $prevKey = $arrayKey[$j-1];
                }
                if ((($key - $prevKey) > 5) || $prevKey == 0)
                {
                    $start = $key - 5;
                    if ($start < 0)
                    {
                        $start = 0;
                    }
                    $end = $key + 5;
                    if ($end > count($searchContent))
                    {
                        $end = count($searchContent)-1;
                    }
                    
                    $tempArray = array();
                    for ($k = $start; $k <= $end; $k++)
                    {
                        if($searchContent[$k] == strip_tags($found[$key]))
                        {
                            $tempArray[] = $found[$key];
                        }
                        else
                        {
                            $tempArray[] = $searchContent[$k];
                        }
                    }
                    $foundString = implode(" ", $tempArray);
                    print($foundString.'...<br />');
                }
            }
            else
            {
                $nextKey = $arrayKey[$j+1];
                if (($nextKey - $key) > 5)
                {
                    $start = $key - 5;
                    if ($start < 0)
                    {
                        $start = 0;
                    }
                    $end = $key + 5;
                    if ($end > count($searchContent))
                    {
                        $end = count($searchContent)-1;
                    }
                    $tempArray = array();
                    for ($k = $start; $k <= $end; $k++)
                    {
                        if($searchContent[$k] == strip_tags($found[$key]))
                        {
                            $tempArray[] = $found[$key];
                        }
                        else
                        {
                            $tempArray[] = $searchContent[$k];
                        }
                    }
                    $foundString = implode(" ", $tempArray);
                    print($foundString.'...<br />');
                }
            }
        }
    }
}
?>


Nu lijkt me dit een goede oplossing voor wat ik wil bereiken, maar er zit volgens mij wel 1 grote drawback aan vast. En dat is volgens mij wanneer er meerdere resultaten met grote content zijn (denk hierbij aan 1000+ woorden). Dan moet de code namelijk elke keer een array maken van alle woorden en daar doorheen gaan loopen. Nu weet ik niet of dat performance gewijs wel zo verstandig is om dit te doen.

Iemand een suggestie, opmerking hierover? Of zelfs een manier waarop het beter kan?

Acties:
  • 0 Henk 'm!

  • Voutloos
  • Registratie: Januari 2002
  • Niet online
Explode op basis van de zoekterm. Explode beide delen op non-word characters en pak de laatste resp eerste x elementen.
Klaar. :Y)

{signature}