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.'&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