note 114650 added to function.substr

From: Date: Mon, 17 Mar 2014 17:34:17 +0000
Subject: note 114650 added to function.substr
Groups: php.notes 
Request: Send a blank email to php-notes+get-198909@lists.php.net to get a copy of this message
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. ---- Server IP: 195.56.44.160 Probable Submitter: 80.98.132.224 ---- Manual Page -- http://php.net/manual/en/function.substr.php Edit -- https://master.php.net/note/edit/114650 Del: integrated -- https://master.php.net/note/delete/114650/integrated Del: useless -- https://master.php.net/note/delete/114650/useless Del: bad code -- https://master.php.net/note/delete/114650/bad+code Del: spam -- https://master.php.net/note/delete/114650/spam Del: non-english -- https://master.php.net/note/delete/114650/non-english Del: in docs -- https://master.php.net/note/delete/114650/in+docs Del: other reasons-- https://master.php.net/note/delete/114650 Reject -- https://master.php.net/note/reject/114650 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#198909) next »