Ergebnisse farbig und als Link anzeigen

Alles, was PHP betrifft, kann hier besprochen werden.

Ergebnisse farbig und als Link anzeigen

Postby juergen&lx » 07. August 2007 20:32

Hallo zusammen,
ich möchte Daten aus der Datenbank als Link anzeigen und gleichzeitig mit dem Suchtext farbig hinterlegen.
Jede der nachstehenden Funktionen funktioniert für sich einwandfrei.
Code: Select all
<?
    /********************************************
     * source:  http://de2.php.net/manual/de/function.eregi-replace.php
     *
     */
     function highlight($word, $subject) {
         $regex_chars = '\.+?(){}[]^$';
         for ($i=0; $i<strlen($regex_chars); $i++) {
             $char = substr($regex_chars, $i, 1);
             $word = str_replace($char, '\\'.$char, $word);
         }
         $word = '(.*)('.$word.')(.*)';
         return eregi_replace($word, '\1<span class="highlight">\2</span>\3', $subject);
     }

    /********************************************
     * source: http://de2.php.net/manual/de/function.preg-replace.php
     * Replace searched words with a
     * <span class="yellow">*</span>
     * for a better view in a resultlist.
     * The var $search is part of a searchstring
     * The var $subject is e.g a $row[field]-var
     * used if searchstring = exact
     */
    function mark1($search,$subject)
        {
       if (empty ($search) ) { return $row;
       }
            $search = preg_replace('/\s\s+/', ' ', $search); //limit blanks=1 between words
            $search = preg_quote($search,'\'');
            $search = preg_quote($search,'\´');
            $words =  explode (' ', trim($search) );
            foreach($words as $word)
                   {
                    $patterns[]='/'.$word.'/i';
                    $replaces[]='<span class="yellow">$0</span>';
                   }
         return  preg_replace($patterns, $replaces, $subject); //nl2br(htmlentities( $subject))
    }

    /********************************************
     * Replace searched words with a <span class=>
     * for a better view in a resultlist.
     * The var $word is part of a searchstring
     * The var $subject is e.g a $row[field]-var
     * used if searchstring = any / all words
     */
    function mark($search,$subject)
        {
       if (empty ($search) ) { return $subject;
       }
            $search = preg_replace('/\s\s+/', ' ', $search);
            $regex_chars = '\.+?(){}[]^$';
            $search = preg_quote($search,'\'');
            $search = preg_quote($search,$regex_chars);
            $words =  explode (' ', trim($search) );
            foreach($words as $word)
                   {
                    $patterns[]= $word;
                    $replaces[]='<span class="yellow">'.$word.'</span> ';
                   }
         return  str_ireplace($patterns, $replaces, $subject);
    }
   
    /********************************************
     * Replace searched words with a
     * <span class="yellow">*</span>
     * for a better view in a resultlist.
     * The var $word is part of a searchstring
     * The var $subject is e.g a $row[field]-var
     * used if searchstring = exact
     */
    function highlight($search,$subject)
        {
       if (empty ($search) ) { return $row;
       }
            $search = preg_replace('/\s\s+/', ' ', $search); //removes blanks
            $regex_chars = '\.+?(){}[]^$';
            $search = preg_quote($search,'\'');
            $search = preg_quote($search,'\´');
            $words =  array (trim($search));
            foreach($words as $word)
                   {
                    $patterns[]= $word;
                    $replaces[]='<span class="yellow">'.$word.'</span> ';
                   }
         return  str_ireplace($patterns, $replaces, $subject);
    }
?>


Die href-Funktion:
Code: Select all
    /*******************************************
     * adds an a_href to one or more given title
     * include an image, divided by ";"
     */
    function title_href($name, $action) {
       if (empty ($name) ) { return $name;
       }
            $name2      = explode (';', $name ) ;
            $href       = array();
            foreach ( $name2 as $partname ) {
                    $partname = trim($partname);
$href[]=' <a target="_self" href=\''.$_SERVER['PHP_SELF'].'?action='.$action.'&amp;title='.$partname.'\'>
<img src=\'img/button_select.png\' width=\'12\' height=\'13\' border=\'0\' alt=\' \'>'.$partname.'</a>';
            }
            return implode( ', ', $href ); 
    }

Dazu der Aufruf:
Code: Select all
$row['h_titel'] = "There´s a wind a-blowin´; Sweet wind";

$row['h_titel'] = title_href( $row['h_titel'], $action ) ;
  switch ($phrase) {
    case 'exact':
      $row['h_titel'] = highlight ( $findtitle, $row['h_titel'] );
    break;
       
    case 'any':
    case 'all':
    default:
      foreach ($words1 as $word) {
        $row['h_titel'] = highlight1 ( $word, $row['h_titel'] );
    break;
  }
echo $row['h_titel'];

Keine der og. Kombinatione funktionert zufriedenstellend.
Wo zum [--Zensur-] liegt mein Fehler

Jürgen
juergen&lx
 
Posts: 154
Joined: 18. February 2004 21:36
Location: in einem Dorf am Rande des Strombergs
Operating System: Linux

Postby Wiedmann » 09. August 2007 18:30

Also ohne Gewähr... Meinst du sowas?
Code: Select all
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
       "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
    <meta http-equiv="content-type" content="text/html; charset=ISO-8859-1">
    <title>Test</title>
    <style type="text/css">
    <!--
        .highlight { color:red; }
    -->
    </style>
</head>

<body>
<?php
error_reporting(E_ALL);

/*
 * Replace searched phrase with a
 * <span class="highlight">*</span>
 */
function highlight_exact($search, $subject) {
    $search = preg_quote(trim($search), '/');

    $result = preg_replace('/\b('.$search.')\b/e',
              "'<span class=\"highlight\">'.htmlspecialchars('\\1').'</span>'",
              $subject);
    return $result;
}

/*
 * Replace any searched word with a
 * <span class="highlight">*</span>
 */
function highlight_any($search, $subject) {
    $words = explode(' ', $search);

    foreach ($words as $key => $value) {
        $words[$key] = preg_quote(trim($value), '/');
    }
    $search = implode('|',$words);

    $result = preg_replace('/\b('.$search.')\b/e',
              "'<span class=\"highlight\">'.htmlspecialchars('\\1').'</span>'",
              $subject);
    return $result;
}

/*
 * adds an a_href to one or more given title
 * include an image, divided by ";"
 */
function title_href($name, $action , $phrase, $findtitle) {
    if ('' === $name) {
        return $name;
    }

    $name = explode (';', $name ) ;
    $href = array();

    foreach ($name as $partname) {
        $partname = trim($partname);
        $uri = htmlspecialchars($_SERVER['PHP_SELF']
             . '?action='.rawurlencode($action)
             . '&title='.rawurlencode($partname));

        switch ($phrase) {
            case 'exact':
                $title = highlight_exact($findtitle, $partname);
                break;
            case 'any':
            case 'all':
                $title = highlight_any($findtitle, $partname);
                break;
            default:
                $title = htmlspecialchars($partname);
                break;
        }

        $href[] = '<a target="_self" href="'.$uri.'">'
                . '<img src="img/button_select.png" width="12" height="13" border="0" alt="">'
                . $title.'</a>';
    }

    $href = implode( ', ', $href );

    return $href;
}

$action = 'show';
$row['h_titel'] = 'There´s a wind a-blowin´; Sweet wind';

echo '<hr>Titel: '.htmlspecialchars($row['h_titel']).'<hr>'.PHP_EOL;
$phrase = 'exact'; // 'exact', 'any', 'all', ''
$findtitle = 'Sweet wind';
echo 'phrase: '.$phrase.'<br>'.PHP_EOL;
echo 'findtitle: '.$findtitle.'<br>'.PHP_EOL;

$links = title_href($row['h_titel'], $action, $phrase, $findtitle);
echo $links.PHP_EOL;

echo '<hr>'.PHP_EOL;
$phrase = 'any'; // 'exact', 'any', 'all', ''
$findtitle = 'a Sweet';
echo 'phrase: '.$phrase.'<br>'.PHP_EOL;
echo 'findtitle: '.$findtitle.'<br>'.PHP_EOL;

$links = title_href($row['h_titel'], $action, $phrase, $findtitle);
echo $links.PHP_EOL;

?>
</body>
</html>
Wiedmann
AF Moderator
 
Posts: 17106
Joined: 01. February 2004 12:38
Location: Stuttgart / Germany

Postby juergen&lx » 10. August 2007 01:24

Hallo Wiedmann,

vielen Dank für die Mühe,
die du mit dem Script hattest.

hab es noch nicht komplett getestet.

Hier habe ich ein ähnliches ausgegraben:

http://aidanlister.com/repos/v/function ... hlight.php
Bereits hinzugefügte html-links werden hier nicht verändert.

Die links hatten immer übel beim highlighten ausgesehen,
oder es wurde nicht alles angezeigt
oder links führten beispielsweise "...titel=<span class="

Danke :lol:
Jürgen
juergen&lx
 
Posts: 154
Joined: 18. February 2004 21:36
Location: in einem Dorf am Rande des Strombergs
Operating System: Linux


Return to PHP

Who is online

Users browsing this forum: No registered users and 1 guest