Awesome q2a theme
Ask us anything
Toggle navigation
Email or Username
Password
Remember
Login
Register
|
I forgot my password
Questions
Unanswered
Tags
Users
Ask a Question
The substr function and strlen in php is not correct work with Russian characters. How to solve the problem?
0
like
0
dislike
41
views
The substr function and strlen in php is not correct work with Russian characters (utf8 encoding). I tried mb_substr also — did not help.
Who will help to solve the problem?
php
asked
Mar 20, 2019
by
alexkl
|
41
views
answer
comment
Please
log in
or
register
to add a comment.
Please
log in
or
register
to answer this question.
5
Answers
0
like
0
dislike
and what is the impropriety then, you can learn ???
answered
Mar 22, 2019
by
DevMan
ask related question
comment
Please
log in
or
register
to add a comment.
0
like
0
dislike
Here for example function:
\r
/**
* shortenText
* @param string
* @param int
* @param bool, default true
* @since 1.0.9
*/
function shortenText( $text, $count, $addbb=true )
{
if( $count > 0 )
{
$totalchars = strlen( $text );
$charcount = 0;
$fulltext = $text;
$buffer = ";
if( $totalchars >= $count )
{
$bbcode = false;
while( strlen( $fulltext ) > 0 )
{
$char = $fulltext{0};
if( $char == '[' ) $bbcode = true;
if( ( $bbcode && $addbb ) || $charcount < $count ) $buffer .= $char;
$fulltext = substr( $fulltext, 1 );
if( !$bbcode ) $charcount++;
if( $char == ']' ) $bbcode = false;
}
$text = $buffer.( $charcount >= $count? '...': ");
}
}
return $text;
}
\r
If $fultext — English letters. Reduces. If the Russian is an empty string
answered
Mar 22, 2019
by
LIAL
ask related question
comment
Please
log in
or
register
to add a comment.
0
like
0
dislike
1. Use mb_substr, mb_strlen with parameter $encoding='utf-8'
2. Do not use $char=$fulltext{0}, replace mb_substr
answered
Mar 22, 2019
by
alexkl
ask related question
comment
Please
log in
or
register
to add a comment.
0
like
0
dislike
iconv_substr($s, $from, $len, 'UTF-8')
iconv_strlen($s, 'UTF-8')
$s{$i} ==> iconv_substr($s, $i, 1, 'UTF-8')
answered
Mar 22, 2019
by
DevMan
ask related question
comment
Please
log in
or
register
to add a comment.
0
like
0
dislike
You have $bbcode always $false, is this normal? Code is certainly fun...
answered
Mar 22, 2019
by
iStyx
ask related question
comment
Please
log in
or
register
to add a comment.
Related questions
0
like
0
dislike
2
answers
Tell RV to correct the error with substr or mb_substr?
asked
May 20, 2019
by
aurusplay
php
0
like
0
dislike
3
answers
As for php 7.3 to set internal encoding to utf-8 to string functions (like strlen) to work properly with many byte encodings?
asked
Aug 3, 2019
by
Anton_volkov
php
unicode
0
like
0
dislike
2
answers
PHP: strlen and the first line from a file
asked
Apr 3, 2019
by
gfan
php
programming
0
like
0
dislike
1
answer
What to use instead of substr?
asked
Aug 2, 2019
by
Malkolm163
php
0
like
0
dislike
2
answers
When in the GET request parameter from Russian characters, the script doesn't work. What is the reason?
asked
Apr 28, 2019
by
trak_ivan
php
telegram
Most popular tags
javascript
php
css
html
jquery
wordpress
python
linux
web-development
mysql
android
windows
java
layout
c#
computer-networks
node.js
cpp
iron
yii
vue.js
1C-Bitrix
react
laravel
django
nginx
system-administration
search-engine-optimization
api
ubuntu
the-it-education.
ajax
sql
programming
hosting
cms
design
apache
google-chrome
bootstrap
Vkontakte
macos
google
network-administration
git
laptops
algorithms
regular-expressions
unity-game-engine
email
angular
database
network-equipment
software
wooсommerce
debian
.net
ios
information-security
video
law-in-it
browsers
books
parsing
wi-fi
game-development
career
htaccess
postgresql
telegram
mikrotik
mobile-development
ruby-on-rails
the-domain-name-system
modx
Yandex
c
json
opencart
Habr
freelance
vpn
asp.net
windows-server
symfony
bots
hard-drives
math
qt
DIY
audio
frontend
payment-system
bash
electronics
gulp.js
user-interface
docker
online-shopping
110,608
questions
257,187
answers
0
comments
40,796
users