Str_replace (or preg_replace?) Accepting and storing characters with an accent

I am developing a French MySQL database for a website that will contain accented characters and caps in some places. This all works great.

Now I have developed a table showing the contents of the database (works fine), and I put a search bar on it. SQL Query for search works as intended (using LIKE, it is case-insensitive and processes accented characters such as their base letter, which is surprisingly exactly what I want).

Here is my problem: I would like to highlight all search instances directly in the table. I partially worked from this:

str_ireplace($_POST["search"], 
             '<span class="highlight">' . $_POST["search"] . "</span>",
             $row['First_Name']);

but these problems arise:

  • It changes caps in my table based on input
  • ( "ecole" "école" ), .

3 . preg_replace(), , , , . , , SQL Query "LIKE", php - .

+4
3

, , , , , PHP , MySQL LIKE.

, , , .

function highlight_substring( $string, $substring )
{
  if( empty( $string ) || empty( $substring ) ) return false;

  $normal = array( 'à', 'é', 'è', 'ê', 'ë', 'î', 'ï', 'ô', 'ò', 'ö', 'û', 'ü', 'ù', 'ç' );
  $flat = array( 'a', 'e', 'e', 'e', 'e', 'i', 'i', 'o', 'o', 'o', 'u', 'u', 'u', 'c' );

  $str = mb_strtolower( $string );
  $str = str_replace( $normal, $flat, $str );

  $sub = mb_strtolower( $substring );
  $sub = str_replace( $normal, $flat, $sub );

  $pos = mb_strpos( $str, $sub );

  if( $pos !== false )
  {
    $var = mb_substr( $string, 0, $pos ).'<span class="highlight">'.mb_substr( $string, $pos, mb_strlen( $substring ) ).'</span>';
    $var .= mb_substr( $string,( bcadd( mb_strlen( $substring ), $pos ) ) );
    $string = $var;
  }

  return $string;
}

;)

echo highlight_substring( 'Allons à l’école !', 'ecole' ); // user input 'ecole'
echo highlight_substring( 'Allons à l’École !', 'ecole' ); // user input 'ecole'
echo highlight_substring( 'Allons à l’école !', 'Ecole' ); // user input 'Ecole'

:

Allons à l’<span class="highlight">école</span> !
Allons à l’<span class="highlight">École</span> !
Allons à l’<span class="highlight">école</span> !
+2

PHP 5.3+, 1 - acutes, ,

mb_regex_encoding('utf-8');
mb_internal_encoding('utf-8');

$row = array('First_Name' => 'some École text with école ecole end of some text ');

function highlightString($string, $word)
{
    $string = iconv('utf-8', 'ISO-8859-1//IGNORE', Normalizer::normalize($string, Normalizer::FORM_D));
    $word = iconv('utf-8', 'ISO-8859-1//IGNORE', Normalizer::normalize($word, Normalizer::FORM_D));
    return mb_ereg_replace_callback('('.$word.')', function ($m) { return '<span class=\"highlight\">'.$m[0].'</span>';}, $string, 'msri'); // it is not very secure to use data from POST directly
}

echo highlightString($row['First_Name'], 'école') . " <br>\n";
echo highlightString($row['First_Name'], 'ecole'). " <br>\n";
+1

Consider using regex to ignore accents. Something like that:

$value = 'É ç école';

echo preg_replace("/&([a-z])[a-z]+;/i", "$1", htmlentities($value));
-1
source

All Articles