Skip to content

Instantly share code, notes, and snippets.

@phpfiddle
Created April 23, 2013 12:12
Show Gist options
  • Save phpfiddle/5443068 to your computer and use it in GitHub Desktop.
Save phpfiddle/5443068 to your computer and use it in GitHub Desktop.
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>PhpFiddle Initial Code</title>
</head>
<body>
<?php
/**
* I have problems with iconv on my local machine so I use this simple
* implementation instead, feel free to use something more complicated or
* robust if your situation requires it:
* http://stackoverflow.com/questions/3542818/remove-accents-without-using-iconv/
*/
function stripAccents($p_sSubject) {
return strtr(
utf8_decode($p_sSubject)
, utf8_decode('àáâãäçèéêëìíîïñòóôõöùúûüýÿÀÁÂÃÄÇÈÉÊËÌÍÎÏÑÒÓÔÕÖÙÚÛÜÝ')
, 'aaaaaceeeeiiiinooooouuuuyyAAAAACEEEEIIIINOOOOOUUUUY'
);
}
function emphasiseWord($p_sSubject, $p_sSearchTerm){
/*
* The part in the regular expression that read "\p{L}\p{M}" makes sure we
* also get all the multibyte characters.
* You can learn more about unicode and regular expressions at:
* http://www.regular-expressions.info/unicode.html
*/
$aSubjects = preg_split('#([^a-z0-9\p{L}\p{M}]+)#iu', $p_sSubject, null, PREG_SPLIT_DELIM_CAPTURE);
foreach($aSubjects as $t_iKey => $t_sSubject){
$sSubject = stripAccents($t_sSubject);
if(stripos($sSubject, $p_sSearchTerm) !== false || mb_stripos($t_sSubject, $p_sSearchTerm) !== false){
$aSubjects[$t_iKey] = '<strong>' . $t_sSubject . '</strong>';
}
}
$sSubject = implode('', $aSubjects);
return $sSubject;
}
// Test
$aTest = array(
'goo' => 'I love Google to make my searches, but I`m starting to worry about privacy.'
, 'peo' => 'people, People, PEOPLE, peOple, people!, people., people?, "people, people" péo'
, 'péo' => 'people, People, PEOPLE, peOple, people!, people., people?, "people, people" péo'
, 'gen' => '"gente", "inteligente", "VAGENS", and "Gente" ...vocês da física que passam o dia protegendo...'
, 'voce' => '...vocês da física que passam o dia protegendo...'
);
$sContent = '<dl>';
foreach($aTest as $t_sSearchTerm => $t_sSubject){
$sContent .= '<dt>' . $t_sSearchTerm . '</dt><dd>' . emphasiseWord($t_sSubject, $t_sSearchTerm) .'</dd>';
}
$sContent .= '</dl>';
echo $sContent;
?>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment