As you noted in one of the comments, you do not need a regular expression for this, since the goal is to find specific lines. Why don't you use explode ? For instance:
$clientName = 'céra'; $this->search = 'cera'; $compareClientName = strtolower(iconv('utf-8', 'ascii//TRANSLIT', $clientName)); $this->search = strtolower($this->search); $pieces = explode($compareClientName, $this->search); if (count($pieces) > 1) { $clientName = implode('<span class="highlight">'.$clientName.'</span>', $pieces); }
Edit:
If your $search variable may contain special characters, why don't you use its translit and use mb_strpos with $offset ? eg:
$offset = 0; $highlighted = ''; $len = mb_strlen($compareClientName, 'UTF-8'); while(($pos = mb_strpos($this->search, $compareClientName, $offset, 'UTF-8')) !== -1) { $highlighted .= mb_substr($this->search, $offset, $pos-$offset, 'UTF-8'). '<span class="highlight">'. mb_substr($this->search, $pos, $len, 'UTF-8').'</span>'; $offset = $pos + $len; } $highlighted .= mb_substr($this->search, $offset, 'UTF-8');
Update 2:
It is important to use the mb_ functions instead of plain strlen , etc. This is because accented characters are stored using two or more bytes; Also always make sure that you are using the correct encoding, look at this, for example:
echo strlen('é'); > 2 echo mb_strlen('é'); > 2 echo mb_internal_encoding(); > ISO-8859-1 echo mb_strlen('é', 'UTF-8'); > 1 mb_internal_encoding('UTF-8'); echo mb_strlen('é'); > 1
source share