@@ -1653,16 +1653,33 @@ public function macro($value, $params, $context)
16531653 */
16541654 public function mark ($ value , $ params , $ context )
16551655 {
1656+ [$ flags , $ params ] = collect ($ params )->partition (
1657+ fn (mixed $ param ): bool => is_string ($ param ) && in_array (strtolower ($ param ), ['substring:true ' , 'substring:false ' ], true )
1658+ );
1659+
1660+ $ substring = ! $ flags ->contains (fn (string $ flag ): bool => strtolower ($ flag ) === 'substring:false ' );
1661+
1662+ $ params = $ params ->values ()->all ();
1663+
16561664 if (! $ words = $ params [0 ] ?? $ context ['get ' ]['q ' ] ?? null ) {
16571665 return $ value ;
16581666 }
16591667
1660- $ params [ 0 ] = collect (preg_split ('/\s+/ ' , $ words ))
1661- ->map (fn ($ word ) => preg_quote ($ word , '/ ' ))
1662- ->filter ()
1668+ $ pattern = collect (preg_split ('/\s+/ ' , $ words ))
1669+ ->map (fn (string $ word ): string => preg_quote ($ word , '/ ' ))
1670+ ->filter (fn ( string $ word ): bool => $ word !== '' )
16631671 ->join ('| ' );
16641672
1665- return $ this ->regexMark ($ value , $ params );
1673+ if ($ substring || $ pattern === '' ) {
1674+ $ params [0 ] = $ pattern ;
1675+
1676+ return $ this ->regexMark ($ value , $ params );
1677+ }
1678+
1679+ // The unicode flag lets the boundaries treat accented and non-Latin letters as word characters.
1680+ $ params [0 ] = '(?<![\p{L}\p{N}_])(?: ' .$ pattern .')(?![\p{L}\p{N}_]) ' ;
1681+
1682+ return $ this ->markWithFlags ($ value , $ params , 'isu ' );
16661683 }
16671684
16681685 /**
@@ -2090,24 +2107,7 @@ public function readTime($value, $params)
20902107 */
20912108 public function regexMark ($ value , $ params )
20922109 {
2093- if (! $ pattern = array_shift ($ params )) {
2094- return $ value ;
2095- }
2096-
2097- $ attributes = $ this ->buildAttributesFromParameters ($ params );
2098-
2099- return Html ::mapText ($ value , function ($ text ) use ($ pattern , $ attributes ) {
2100- $ text = Html ::decode ($ text );
2101-
2102- return Str ::mapRegex ($ text , "/( {$ pattern })/is " , function ($ part , $ match ) use ($ attributes ) {
2103- $ part = Html ::entities ($ part );
2104- if ($ match ) {
2105- $ part = '<mark ' .Html ::attributes ($ attributes ).'> ' .$ part .'</mark> ' ;
2106- }
2107-
2108- return $ part ;
2109- });
2110- });
2110+ return $ this ->markWithFlags ($ value , $ params , 'is ' );
21112111 }
21122112
21132113 /**
@@ -3272,6 +3272,28 @@ private function buildAttributesFromParameters($params, $delimiter = ':')
32723272 return $ attributes ;
32733273 }
32743274
3275+ private function markWithFlags ($ value , $ params , string $ flags )
3276+ {
3277+ if (! $ pattern = array_shift ($ params )) {
3278+ return $ value ;
3279+ }
3280+
3281+ $ attributes = $ this ->buildAttributesFromParameters ($ params );
3282+
3283+ return Html ::mapText ($ value , function ($ text ) use ($ pattern , $ attributes , $ flags ) {
3284+ $ text = Html ::decode ($ text );
3285+
3286+ return Str ::mapRegex ($ text , "/( {$ pattern })/ {$ flags }" , function ($ part , $ match ) use ($ attributes ) {
3287+ $ part = Html ::entities ($ part );
3288+ if ($ match ) {
3289+ $ part = '<mark ' .Html ::attributes ($ attributes ).'> ' .$ part .'</mark> ' ;
3290+ }
3291+
3292+ return $ part ;
3293+ });
3294+ });
3295+ }
3296+
32753297 private function getMathModifierNumber ($ params , $ context )
32763298 {
32773299 $ number = $ params [0 ];
0 commit comments