note 114650 deleted from function.substr by cmb

From: Date: Sun, 27 Oct 2019 09:05:14 +0000
Subject: note 114650 deleted from function.substr by cmb
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-212820@lists.php.net to get a copy of this message
Note Submitter: koczkahun at gmail dot com ---- For those who cannot use mb_substr for any reasons, here is a solution for Unicode strings: function substr_unicode($str, $s, $l = null) { $r = '/^.{'.(int)$s.'}(.'; $r .= ($l === null) ? '*)$' : '{'.(int)$l.'})'; $r .= '/su'; preg_match($r, $str, $o); return $o[1]; } It is EXTREMELY fast compared to the array_slice + join method suggested before, and moderately slower than mb_substr. Note: you cannot use negative values. Note 2: use mb_substr whenever you can. Only use this workaround if you can not use the library.

« previous php.notes (#212820) next »