/*******************************************************************
* This function highlight a wanted keyword in a text.
*
* @param string $strHaystack raw text
* @param string $strNeedle wanted keyword(s)
* @param string $strColor color in hexa decimal
* @param string $strStyle color style (color|background-color)
* @param bool $bCase case sensitive (True|False)
* @return string
*******************************************************************/
function searchmarker ( $strHaystack, $strNeedle, $strColor = '', $strStyle = '', $bCase = False ) {
(string) $strModifier = '';
if ( empty ( $strColor ) ) $strColor = '#ff0000';
if ( empty ( $strStyle ) ) $strStyle = 'color';
// Modifikator "i": Gross- und Kleinschreibung ignorieren.
if ( $bCase ) $strModifier = 'i';
(string) $strQuotedNeedle = preg_quote ( $strNeedle, '/' );
(string) $strPattern = '/' . $strQuotedNeedle . '/' . $strModifier;
(string) $strReplacement = '<span style="'.$strStyle.': '.$strColor.';">$0</span>';
return preg_replace ( $strPattern, $strReplacement, $strHaystack );
}
// @example
$cfg['search'] = array( 'case' => False,
'style' => 'background-color',
'color' => '#ffff00'
);
(array) $data = array( 'mschulz', 'tklets', 'eschulz', 'pschulz' );
(string) $regexp = 'Ul';
foreach ( $data as $vars ) {
echo searchmarker( $vars, $regexp, $cfg['search']['color'], $cfg['search']['style'], $cfg['search']['case'] )."<br/>";
}