[php] preg_replace probleempje

Pagina: 1
Acties:

  • chuxiej
  • Registratie: Februari 2001
  • Laatst online: 13-07-2020
Hoi,

Ik ben bezig met een gastenboek en nu met de functie words filter.

Nu wil ik het via preg_replace gaan doen.
Het probleem is dat de hele text ($ouput) weg gehaald word en er komt niets voor in de plaats

Hier is de functie:
PHP:
1
<?function words_replace($output) {        // opens the smiley data file    $bw_file = file('templetates/words.dat');        // explode's the line (word|replacement) and puts it in an array    while(list($line_num,$line) = @each($bw_file)) {        $words = explode("|",$line);                $search[] = "'" . $words[0] . "'si";        $replace[] = $words[1];    }        $output = preg_replace($search,$replace ,$ouput);        return $output;}?>

De inhoud van words.dat is:
woord|replacement

www.dannyhiemstra.nl


  • drm
  • Registratie: Februari 2001
  • Laatst online: 09-06-2025

drm

f0pc0dert

waarom gebruik je dan preg_replace ipv str_replace :?

Music is the pleasure the human mind experiences from counting without being aware that it is counting
~ Gottfried Leibniz


  • chuxiej
  • Registratie: Februari 2001
  • Laatst online: 13-07-2020
omdat str_replace hoofdletter gevoelig is?

www.dannyhiemstra.nl


  • Freak_NL
  • Registratie: Juli 2000
  • Laatst online: 20-07 09:47
Typfoutjes..

$ouput zal wel $output moeten zijn (in preg_replace() functie).

Templates schrijf je trouwens zo, uh, templates dus. :)

  • Dennis
  • Registratie: Februari 2001
  • Laatst online: 16:05
FireFoxx:
omdat str_replace hoofdletter gevoelig is?
code:
1
2
3
4
5
6
7
8
9
10
11
12
13
function stri_replace( $find, $replace, $string )
{
    $parts = explode( strtolower($find), strtolower($string) );

    $pos = 0;

    foreach( $parts as $key=>$part ){
      $parts[ $key ] = substr($string, $pos, strlen($part));
      $pos += strlen($part) + strlen($find);
      }

    return( join( $replace, $parts ) );
}

Verwijderd

handige post dit, alleen ik kom hier effe niet uit nog.

Kijk dit zijn nu mijn twee functies om vanuit een zoekcommando delen van namen te highlighten:

PHP:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
// STRI_REPLACE
function stri_replace( $find, $replace, $string )
{
    $parts = explode( strtolower($find), strtolower($string) );

    $pos = 0;

    foreach( $parts as $key=>$part ){
      $parts[ $key ] = substr($string, $pos, strlen($part));
      $pos += strlen($part) + strlen($find);
      }

    return( join( $replace, $parts ) );
}


// HIGHLIGHT
function highlight($tekst, $waarde) {
    $hlwaarde = "<span class='highlight'>$waarde</span>";
    $tekst = stri_replace($waarde, $hlwaarde, $tekst);
    
    return $tekst;
}


Nu gebeurt het dus alleen, dat als ik zoek op bijvoorbeeld "jan", hij terugkomt met

code:
1
<span class='highlight'>jan</span> Pietersen


terwijl de oorspronkelijk gevonden naam (in mysql) Jan Pietersen was. Ik weet wel waardoor dat komt natuurlijk, maar ik kom er effetjes niet uit hoe ik die functies moet aanpassen. De oplossing zit hem in gevorderd replacen, maar zover gaat mijn kennis helaas nog niet...

Voor alle duidelijkheid, dit is wat ie moet weergeven als ik zoek op "jan"

code:
1
<span class='highlight'>Jan</span> Pietersen


hellup :/

[ Voor 20% gewijzigd door Verwijderd op 12-11-2003 23:43 ]


  • Jordi
  • Registratie: Januari 2000
  • Niet online

Jordi

#1#1

PHP:
1
$tekst = preg_replace("/(".preg_quote($waarde, "/").")/i", "<span class='highlight';>$1</span>", $tekst);

Die custom stri_replace is niet nodig en je case wordt zo behouden (en of preg_replace trager is dan stri_replace betwijfel ik ten zeerste).

Het zal wel niet, maar het zou maar wel.


Verwijderd

precies goed, bedankt!
Pagina: 1