3.137.183.14 |    

Navigation

Google Advertisement

This function highlight a wanted keyword in a text.

searchmarker
  1. /******************************************************************* 
  2.  * This function highlight a wanted keyword in a text.
  3.  * 
  4.  * @param string $strHaystack raw text
  5.  * @param string $strNeedle wanted keyword(s)
  6.  * @param string $strColor color in hexa decimal
  7.  * @param string $strStyle color style (color|background-color)
  8.  * @param bool $bCase case sensitive (True|False)
  9.  * @return string
  10.  *******************************************************************/
  11.  
  12. function searchmarker ( $strHaystack, $strNeedle, $strColor = '', $strStyle = '', $bCase = False ) {
  13.  
  14. (string) $strModifier = '';
  15.  
  16. if ( empty ( $strColor ) ) $strColor = '#ff0000';
  17. if ( empty ( $strStyle ) ) $strStyle = 'color';
  18.  
  19. // Modifikator "i": Gross- und Kleinschreibung ignorieren.
  20. if ( $bCase ) $strModifier = 'i';
  21.  
  22. (string) $strQuotedNeedle = preg_quote ( $strNeedle, '/' );
  23. (string) $strPattern = '/' . $strQuotedNeedle . '/' . $strModifier;
  24. (string) $strReplacement = '<span style="'.$strStyle.': '.$strColor.';">$0</span>';
  25. return preg_replace ( $strPattern, $strReplacement, $strHaystack );
  26.  
  27. }
  28.  
  29.  
  30. // @example
  31. $cfg['search'] = array( 'case' => False,
  32. 			'style' => 'background-color',
  33. 			'color' => '#ffff00'
  34. );
  35.  
  36. (array) $data = array( 'mschulz', 'tklets', 'eschulz', 'pschulz' );
  37. (string) $regexp = 'Ul';
  38.  
  39. foreach ( $data as $vars ) {
  40. 	echo searchmarker( $vars, $regexp, $cfg['search']['color'], $cfg['search']['style'], $cfg['search']['case'] )."<br/>";
  41. }
Parsed in 0.011 seconds at 135.00 KB/s

Search
 
Full text search by name and content of a snippet.

User online
There are 1 users online.

Tags Cloud

Latest snippets
str2seconds
(Bash::Function)
is_integer
(Bash::Function)
file_rotate
(Bash::Function)
confirm
(Bash::Function)
is_workingtime
(Bash::Function)
last day of last month
(Bash::Snippets)
crypt_apr1_md5
(PHP::Function)
crypt_apr1_md5
(Perl::Function)
transparent
(CSS)
rfc2822Toiso8601
(PHP::Function)