note 129157 added to function.str-ireplace

From: Date: Fri, 12 Jan 2024 02:05:37 +0000
Subject: note 129157 added to function.str-ireplace
Groups: php.notes 
Request: Send a blank email to php-notes+get-221111@lists.php.net to get a copy of this message
Here is a final version modified from an early one to get a 100% working textHighlight for utf-8 encoded text. It is accents and case insensitive and respects original case and accents. You can change highlight tags according to your need. function highlightCaseAccentInsensitive( $haystack, $needle) { $haystack_na = remove_accents($haystack); $needle_na = remove_accents($needle); $ind = mb_stripos($haystack_na, $needle_na,0,"utf-8"); $len = mb_strlen($needle_na, "utf-8"); if($ind !== false){ return mb_substr($haystack, 0, $ind,"utf-8") . "<span class='your-strong-class'>" . mb_substr($haystack, $ind, $len,"utf-8") . "</span>" . highlightCaseAccentInsensitive(mb_substr($haystack, $ind + $len,NULL,"utf-8"), $needle); } else return $haystack; } function remove_accents($string) { if ( !preg_match('/[\x80-\xff]/', $string) ) return $string; $chars = array( // Decompositions for Latin-1 Supplement chr(195).chr(128) => 'A', chr(195).chr(129) => 'A', chr(195).chr(130) => 'A', chr(195).chr(131) => 'A', chr(195).chr(132) => 'A', chr(195).chr(133) => 'A', chr(195).chr(135) => 'C', chr(195).chr(136) => 'E', chr(195).chr(137) => 'E', chr(195).chr(138) => 'E', chr(195).chr(139) => 'E', chr(195).chr(140) => 'I', chr(195).chr(141) => 'I', chr(195).chr(142) => 'I', chr(195).chr(143) => 'I', chr(195).chr(145) => 'N', chr(195).chr(146) => 'O', chr(195).chr(147) => 'O', chr(195).chr(148) => 'O', chr(195).chr(149) => 'O', chr(195).chr(150) => 'O', chr(195).chr(153) => 'U', chr(195).chr(154) => 'U', chr(195).chr(155) => 'U', chr(195).chr(156) => 'U', chr(195).chr(157) => 'Y', chr(195).chr(159) => 's', chr(195).chr(160) => 'a', chr(195).chr(161) => 'a', chr(195).chr(162) => 'a', chr(195).chr(163) => 'a', chr(195).chr(164) => 'a', chr(195).chr(165) => 'a', chr(195).chr(167) => 'c', chr(195).chr(168) => 'e', chr(195).chr(169) => 'e', chr(195).chr(170) => 'e', chr(195).chr(171) => 'e', chr(195).chr(172) => 'i', chr(195).chr(173) => 'i', chr(195).chr(174) => 'i', chr(195).chr(175) => 'i', chr(195).chr(177) => 'n', chr(195).chr(178) => 'o', chr(195).chr(179) => 'o', chr(195).chr(180) => 'o', chr(195).chr(181) => 'o', chr(195).chr(182) => 'o', chr(195).chr(182) => 'o', chr(195).chr(185) => 'u', chr(195).chr(186) => 'u', chr(195).chr(187) => 'u', chr(195).chr(188) => 'u', chr(195).chr(189) => 'y', chr(195).chr(191) => 'y' // Decompositions for Latin Extended-A // You can easily find the rest of char list in google search for this function (I am missing place in this post) ); $string = strtr($string, $chars); return $string; } ---- Server IP: 45.112.84.4 Probable Submitter: 45.91.157.134 (proxied: 24.212.81.48) ---- Manual Page -- https://php.net/manual/en/function.str-ireplace.php Edit -- https://main.php.net/note/edit/129157 Del: integrated -- https://main.php.net/note/delete/129157/integrated Del: useless -- https://main.php.net/note/delete/129157/useless Del: bad code -- https://main.php.net/note/delete/129157/bad+code Del: spam -- https://main.php.net/note/delete/129157/spam Del: non-english -- https://main.php.net/note/delete/129157/non-english Del: in docs -- https://main.php.net/note/delete/129157/in+docs Del: other reasons-- https://main.php.net/note/delete/129157 Reject -- https://main.php.net/note/reject/129157 Search -- https://main.php.net/manage/user-notes.php

« previous php.notes (#221111) next »